[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Search for AD users with 2 spaces in their name.

Posted on 2016-11-03
5
Medium Priority
?
95 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 1000 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 85

Accepted Solution

by:
oBdA earned 1000 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

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.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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