Link to home
Start Free TrialLog in
Avatar of DHPBilcare
DHPBilcareFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell Script to identify users with the same display name

I need to write and automate a power shell script to identify any users in active directory thay have a duplicate Display Name.  Any help would be appreciated.
Avatar of oBdA
oBdA

Like that, for example:
$duplicates = @{}
Get-ADUser -Filter "DisplayName -like '*'" -ResultSetSize $null -Property DisplayName | ForEach-Object {
	If (-not $duplicates.ContainsKey($_.DisplayName)) {
		$duplicates[$_.DisplayName] = @()
	}
	$duplicates[$_.DisplayName] += $_.SamAccountName
}
$duplicates.GetEnumerator() |
	Where-Object {$_.Value.Count -gt 1} |
	Select-Object -Property @{n='DisplayName'; e={$_.Name}}, @{n='Used by SamAccountNames'; e={$_.Value -join ', '}} |
	Export-Csv -NoTypeInformation -Path "C:\Temp\Duplicates.csv"

Open in new window

Avatar of DHPBilcare

ASKER

Thanks for that.  Could I just copy that directly into a .BAT file to run from the Active Directory server?
That's PowerShell, so it's .ps1. Otherwise yes, save it as it is.
Thanks.  Done that but how do I enforce it to run on the server as administrator?  Currently when I open the file with PowerShell it runs but I don’t see the output CSV file.
As usual: you right-click the PowerShell shortcut and choose "Run as administrator". These are just read operations, so unless you explicitly restricted access, a regular user should be able to run it as well.
If it doesn't produce output (no error messages either?), then there probably aren't any duplicates.
Setup a test user with a known duplicate DisplayName and run it again.
Thanks for you help.  It is giving a red error but it flages on the screen and is too quick to read.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect!  Much appreciated, all now working.  :)