Solved

Search for AD users with 2 spaces in their name.

Posted on 2016-11-03
5
53 Views
Last Modified: 2016-11-03
Some of our ad users names have 2 spaces in between the first and last name. How can I do a search for users with 2 spaces in their names? I would like to replace the 2 spaces with only one space after so if you have a solution for that also that would be helpful.
0
Comment
Question by:Roccat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Expert Comment

by:Dustin Saunders
ID: 41872221
Do you mean in the display name?  Or are there spaces before/after the firstname surname items?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41872231
Depends on which attribute you want to validate.. If you are taking about DisplayName, you can try..
Get-ADUser -f * -pr DisplayName | ?{$_.DisplayName -match "[^\s]\s{2,}[^\s]"} | Select DisplayName 

Open in new window

Following code can be used to replace more than one spaces in the displayname with single space.
Get-ADUser -f * -pr DisplayName | ?{$_.DisplayName -match "[^\s]\s{2,}[^\s]"} | %{
	$DisplayName = $_.DisplayName -replace "\s+"," "
	Set-ADUser -Identity $_.sAMAccountName -DisplayName $DisplayName
}

Open in new window

0
 

Author Comment

by:Roccat
ID: 41872245
Yes I am talking about the display name and your script does find them subsun
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 250 total points
ID: 41872254
You can test the code for a couple of users with more than one space in their displayname.. Example..
"UserA","UserB" | Get-ADUser -pr DisplayName | ?{$_.DisplayName -match "[^\s]\s{2,}[^\s]"} | %{
	$DisplayName = $_.DisplayName -replace "\s+"," "
	Set-ADUser -Identity $_.sAMAccountName -DisplayName $DisplayName
}

Open in new window

0
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 41872257
This fixes both displayName and DistinguishedName (and filters in AD, not in Powershell).
It's in test mode, remove the two "-WhatIf" to run it for real.
Get-ADUser -Filter "(displayName -like '*  *') -or (cn -like '*  *')" -Property DisplayName, cn | ForEach-Object {
	"Processing $($_.SamAccountName) ..." | Write-Host
	If ($_.displayName -like '*  *') {
		"`t- fixing displayName ..." | Write-Host
		Set-ADUser -Identity $_.DistinguishedName -DisplayName ($_.displayName -replace ' +', ' ') -WhatIf
	}
	If ($_.cn -like '*  *') {
		"`t- fixing distinguishedName ..." | Write-Host
		Rename-ADObject -Identity $_.DistinguishedName -NewName ($_.cn -replace ' +', ' ') -WhatIf
	}
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question