Eitan
Eitan

Reputation: 1504

How to append string literals to properties returned from a Select-Object command

How do you concatenate a string literal and parameter in a Select-Object.

For Example I have:

Get-AdUser -filter * | Select-Object 'sip'+SamAccountName,Name, Email, etc

Basically I want to return the property SamAccountName from the AdUser with the string literal sip before it.

I tried everything I can think of.

Thanks!

Edit: added parameters and multiple users

Upvotes: 15

Views: 33372

Answers (2)

zdan
zdan

Reputation: 29470

You can specify an expression in a script block to do this. Like this:

 Get-AdUser [email protected] | select-object {"sip"+$_.SamAccountName}

Though if you do it this way, the name of resultant property looks a bit weird. To specify the property name as well, enclose the script block as part of a hashtable that specifies the name of the new property as well as the scriptblock expression to generate it. It would look like this:

Get-AdUser [email protected] | select-object @{name="SamAccountName"; expression={"sip"+$_.SamAccountName}}

Upvotes: 22

Adam Maras
Adam Maras

Reputation: 26883

You don't need to use Select-Object, because you're not trying to create a new object with specific properties; you're trying to select a single property (SamAccountName) and manipulate it.

"sip" + (Get-ADUser [email protected]).SamAccountName

Upvotes: 2

Related Questions