Search code examples
powershellactive-directoryselect-object

PowerShell: append string to result of Get-ADUser query


I want to attach a string to a Get-ADUser object.

$VALUE =  "Something : Else " 

$RESULT = Get-ADUser -Filter 'givenName -eq "Joe" ' -Property * | 
             Select-Object -Property givenName, Surname, "$VALUE"

I got some bracket { } instead of "Else"

PS C:\> $RESULT 
givenName                  : Joe
Surname                    : Black
Something : Else           : {}

But I want this here!

PS C:\> $RESULT 
givenName                  : Joe
Surname                    : Black
Something                  : Else

I try to put the Get-ADUser object into a string array and then attach $VALUE but that doesn't works.

Has anybody a idea?

Because I have to save it into a CSV file.

$RESULT | Export-CSV $DESTINATION -NoTypeInformation -Encoding UTF8 -Delimiter ';'

Solution

  • The syntax for calculated properties uses hashtables with a Name/Label and Expression key (which you can shorten to n and e).

    Name must be a string (the name of your custom property), and Expression must be a scriptblock.

    # long version
    Select-Object -Property GivenName, Surname, @{Name = "Something"; Expression = {"Else"}}
    # short version
    select GivenName, Surname, @{n="Something";e={"Else"}}