Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have bunch of users which DisplayNames I changed mistakenly to some string (for example: xyz)

But there is one field called Name which has FirstName and Lastname. What I want is take all users who has xyz in it and them change DisplayName to the corresponding Name.

Get-Mailbox -ResultSize Unlimited -Filter {DisplayName -like "*xyz*"} |

This gives me the array I need whose DisplayNames i need to change. What I add after the "pipe" ?

I googled a lot, however couldn't come up with the answer.

I tried:

Set-Mailbox -DisplayName $_.Name

However it says

Cannot bind parameter 'DisplayName' to the target. .... cannot be null/empty.

How can I set DisplayName to the same value as Name?

share|improve this question
add comment

2 Answers

There's no $_ variable when piping from one cmdlet to the next directly. So Set-Mailbox is seeing a $null for the DisplayName parameter and failing. Try wrapping the Set-Mailbox command in a ForEach-Object call (alias %) to get that $_ variable, like so:

Get-Mailbox -ResultSize Unlimited -Filter {DisplayName -like "*xyz*"} | % {Set-Mailbox  -Identity $_.PrimarySmtpAddress -DisplayName $_.Name}
share|improve this answer
    
I did the following: Get-Mailbox -ResultSize Unlimited -Filter {DisplayName -like "*Firstname Lastname*"} | % {Set-Mailbox -DisplayName $_.Name} however –  epema Mar 15 '13 at 3:31
    
it says Pipeline not executed because a pipeline is already executin –  epema Mar 15 '13 at 3:41
    
Sorry, you DO need "-Identity $_.PrimarySmtpAddress" in the Set-Mailbox call. I will edit the answer to reflect the full command. –  jbsmith Mar 15 '13 at 16:46
add comment

Here is the solution:

$mbxs = Get-Mailbox -ResultSize Unlimited -Filter {DisplayName -like "*FirstName Lastname*"}
$mbxs | foreach { %{Set-Mailbox -Identity $_.PrimarySmtpAddress -DisplayName $_.Name}}

Thanks all!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.