1

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 ';'
post4dirk
  • 313
  • 4
  • 16

1 Answers1

2

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"}}
marsze
  • 15,079
  • 5
  • 45
  • 61
  • 1
    2 schools... name or label :) I prefer label, but you are right, name seems more suitable :) – CFou Nov 06 '20 at 15:54
  • 2
    Excellent answer, but one caveat - `Expression` value can also be a `[string]`, in which case it'll evaluate to the value of an existing named property (useful for "renaming" properties) – Mathias R. Jessen Nov 06 '20 at 16:24