Solved

Search for AD users with 2 spaces in their name.

Posted on 2016-11-03
5
43 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 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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 previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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