-1

So I have this:

get-ADUser -SearchRoot 'boogers.com/Locations/Kleenex' | 
Where-Object { $_.TITLE -ne 'Snot' } | 
Select LastName,FirstName,Description,SamAccountName,Department,TITLE, @{Name='bs_value';Expression=@{Name='TITLE'; Expression={(Get-Culture).TextInfo.ToTitleCase($_.Title.ToLower())}}

And it works great, until, oh I dunno, I want another calculated field...then it becomes JUST TOO LONG!

So I write a function:

function tc($name, $str) {
    return @{Name=$name; Expression={(Get-Culture).TextInfo.ToTitleCase($str.ToLower())}}
}

Seems reasonable enough, no?

But if I run the function:

$bob = tc 'Title' 'bob'

I get:

Name                           Value
----                           -----
Expression                     (Get-Culture).TextInfo.ToTitleCase($str.ToLower())
Name                           Title

Instead of...

Name                           Value
----                           -----
Expression                     "Bob"
Name                           Title

I just want the code to be shorter! I'm using a computer!

JJJ
  • 32,902
  • 20
  • 89
  • 102
leeand00
  • 25,510
  • 39
  • 140
  • 297
  • rewrite your `tc` func to just have the ToTitleCase stuff. then call it in the `Expression =` section of your calculated property. – Lee_Dailey Aug 16 '19 at 02:54
  • @Lee_Dailey I think, I tried that. I just didn’t put it here. It’s been a bit of a long day. I end up with the data structure instead of the string. – leeand00 Aug 16 '19 at 03:18

1 Answers1

1

since you didn't show how you were calling the modified function i mentioned in my comment, i presume you were not using it correctly. [grin] your function is ... rather peculiar, so i rewrote it to work in a more normal manner.

function ConvertTo-TitleCase ([string]$InString)
    {
    (Get-Culture).TextInfo.ToTitleCase($InString.ToLower())
    }

Get-LocalUser |
    Where-Object {
        $_.Name -notmatch "Admin|Guest|Home|$env:USERNAME"
        } | 
    Select-Object -Property Name, FullName, Enabled,
        @{Name='bs_value';Expression={ConvertTo-TitleCase -InString $_.Description}}

output from my local use list ...

Name   FullName        Enabled bs_value                                   
----   --------        ------- --------                                   
22     2 Digit 2          True The Digit 2 Twice                          
TooToo Too Also Too       True The Word For Also, Repeated Twice          
ToTo   To Thataway To     True The Destination Designator, Two Times.     
Tutu   Tutu Dress Tutu    True The Ballet Apparel For Ladies.             
TwoTwo Two Number Two     True Repeating The Name Of The Number After One.
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26