Script needed to remove space in alias

Exchange 2007 no longer supports a space in the alias, so I need to remove it from about 200 DL's. I found the following article: http://www.exchangeninjas.com/RecipientValidation which provided me with the LDAP query to find them all, but now I need the code to remove the space.

I'm pretty new to Powershell, can anyone tell me the code for this?

 - Find all recipients with space/s in alias
 - Take results and remove space/s

thanks!
wallakylAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BSonPoshCommented:
This should do what you want...

If you run it the first time and it does what you expect remove the # from the $user.SetInfo() line.
$filter = "(&(objectclass=*)(mailnickname=* *))"
$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"",$filter)
$searcher.pagesize = 1000
$users = $searcher.findall() | %{$_.GetDirectoryEntry()}
foreach($user in $users)
{
   Write-Host " + Processing User [$($user.DisplayName)]"
   $oldalias = $user.mailnickname[0] 
   Write-Host "   - Old Alias [$oldalias]"
   $newalias = $oldalias -replace "\s",""
   Write-host "   - New Alias to $newalias"
   $user.put("mailnickname",$newalias)
   #$user.SetInfo()
}

Open in new window

0
wallakylAuthor Commented:
BSonPosh,

Thanks for your help. Is there anyway to restrict this to just mail recipeints? Maybe with a Get-Recipient cmdlet? The reason I ask is because this query actually includes some of the default groups, like Offline Address Book and Schedule+ Free Busy Information. See screenshot below.

Thanks.
screenshot.JPG
0
BSonPoshCommented:
Try this... I think it will do it for you.

NOTE: same as before... remove the #
$Recipients = Get-Recipient -Filter "alias -like '* *'" -verb:$false
foreach($Recipient in $Recipients)
{
   $user = [ADSI]"LDAP://$($Recipient.DistinguishedName)"
   Write-Host " + Processing User [$($user.DisplayName)]"
   $oldalias = $user.mailnickname[0] 
   Write-Host "   - Old Alias [$oldalias]"
   $newalias = $oldalias -replace "\s",""
   Write-host "   - New Alias to $newalias"
   $user.put("mailnickname",$newalias)
   #$user.SetInfo()
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

wallakylAuthor Commented:
Getting there :).

I am still seeing those built-in "users". When looking closer, it appears that they are all Public Folders. Do you know how to add a line that does all of the above, except for objects with type = Public Folder?

Thanks, I really appreciate the help.
0
BSonPoshCommented:
You can try this.. I do use public folders so I cannot test.

Notice the -filter parameter to Get-Recipient. That is what you need to play with


Param([Switch]$whatif)
$Recipients = Get-Recipient -Filter "(alias -like '*') -and (RecipientType -ne 'PublicFolder')" -result Unlimited
if(!$Recipients){Write-Host "No Recipients found with spaces";return}
 
foreach($Recipient in $Recipients)
{
   $user = [ADSI]"LDAP://$($Recipient.DistinguishedName)"
   Write-Host " + Processing User [$($user.DisplayName)]"
   $oldalias = $user.mailnickname[0] 
   Write-Host "   - Old Alias [$oldalias]"
   $newalias = $oldalias -replace "\s",""
   Write-host "   - New Alias to $newalias"
   $user.put("mailnickname",$newalias)
   if(!$whatif){$user.SetInfo()}
}

Open in new window

0
BSonPoshCommented:
btw... I made it into a script so you save it as a .ps1 file and pass -whatif to test.
0
wallakylAuthor Commented:
Thanks for the help. In the end, I ended up just manually changing them because I was spending more time trying to script it then it would take to manually do it. I appreciate the help though.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.