Hello!
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.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.