Solved

Search for AD users with 2 spaces in their name.

Posted on 2016-11-03
5
18 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
5 Comments
 
LVL 12

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 83

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now