We help IT Professionals succeed at work.

Powershell: How do I modify the output of a string column?

I am trying to write a powershell script to export all the users with a primary SMTP address.  However, I need to modify the Displayname to append a string, such as:   DisplayName + " (LMC)"

How do I append a string to the select-object column?

Thanks!!

Get-Recipient -resultsize 5| Where-Object {$_.RecipientType -eq 'UserMailbox'} | Select-Object FirstName, LastName, DisplayName, @{Name="PrimarySmtpAddress";Expression={$_.PrimarySmtpAddress}}

Open in new window

Comment
Watch Question

I figured it out, although I don't know what that I had to set a variable equal to the column and append the string to the variable.  It would not let me append the string directly to the column output.

...go figure...this works!
$colitems = Get-Recipient -resultsize 5| Where-Object {$_.RecipientType -eq 'UserMailbox'} | Select-Object FirstName, 

LastName, DisplayName, PrimarySmtpAddress
 



foreach ($colitem in $colitems){
	$FN = $colitem.FirstName
	$LN = $colitem.LastName
	$DN = $colitem.DisplayName
	$DN = "$DN (LMC)"
	$EM = $colitem.PrimarySmtpAddress
	$EM = "$EM"
	$Rec = "$FN`t$LN`t$DN`t$EM"
	$Rec
}

Open in new window

Author

Commented:
I found my own answer before anyone could respond.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Very bad method. You are generating a concatenated string, which cannot be processed any further as an object with properties. You are better off with this one:
Get-Recipient -resultsize 5 | 
  Where-Object {$_.RecipientType -eq 'UserMailbox'} |
  Select-Object FirstName, LastName, 
       @{n="DisplayName"; e={$_.DisplayName + " (LMC)"}}, 
       @{Name="PrimarySmtpAddress";Expression={$_.PrimarySmtpAddress}}

Open in new window

and I suppose that is what you had in mind.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
BTW, why do you use an expression for PrimarySmtpAddress, though you do not change anything? You could just have select PrimarySmtpAddress then.

Author

Commented:
I wasn't able, or don't know how to append a string to it.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
I've showed you how above. @{n=...; e=...}  and @{name=...; expression=...}  are the same, should you not have seen that already. And the expression part is exactly that - an expression, where $_ represents the current object (the single get-recipient record currently processed).