troubleshooting Question

Powershell ActiveDirectory Rename SamAcctName

Avatar of CLR Benjamin
CLR Benjamin asked on
Active DirectoryPowershell
20 Comments1 Solution1665 ViewsLast Modified:

I work on a powershell script for my Active Directory.
My mission is simple: Search every samaccountname contains uppercase and export it to a CSV and then rename the samaccountname in lowercase.

And it work with that code for extract samaccountname in uppercase :  
Get-ADUser -filter * -Searchbase 'OU=users,DC=contoso,DC=com' | ? {$_.sAMAccountname -cmatch'^[a-z A-Z .]+$'}  | Select Name | Export-csv -path C:\Users\me\username.csv -NoTypeInformation

Why "-cmatch'^[a-z A-Z .]+$'"? Cause all of my samaccountname are written like that: x.XXxxx .

Next step, i try to rename in lowercase all the samaccountname in the list of username.csv contained in Active DIrectory.
And i'm blocked with that code :
Import-Csv C:\Users\me\username.csv | foreach{Set-ADUser -identity $_-SamAccountName  $_.sAMAccountName.tolower()}
It doesn't work.

Can you help me? Im a newbie.

The hardest point is after executing that script in powershell,  i want to see the result in the AD.
For example: There is an oldSamAccountName: J.smiTH , after executing the script, im able to see in AD (not in the powershell console) the new Account name: j.smith in Properties->Account. Is it possible?

Thanks in advance.
Joshua Grantom
Systems Administrator

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2014

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 20 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 20 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros