?
Solved

Search for AD users with 2 spaces in their name.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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