1

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?

jscott
  • 24,484
  • 8
  • 79
  • 100
ysakiyev
  • 263
  • 1
  • 5
  • 12

2 Answers2

2

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}
jbsmith
  • 1,301
  • 7
  • 13
  • I did the following: `Get-Mailbox -ResultSize Unlimited -Filter {DisplayName -like "*Firstname Lastname*"} | % {Set-Mailbox -DisplayName $_.Name}` however – ysakiyev Mar 15 '13 at 03:31
  • it says `Pipeline not executed because a pipeline is already executin` – ysakiyev Mar 15 '13 at 03: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
0

Here is the solution:

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

Thanks all!

ysakiyev
  • 263
  • 1
  • 5
  • 12