?
Solved

Powershell ActiveDirectory Rename SamAcctName

Posted on 2014-11-14
20
Medium Priority
?
1,258 Views
Last Modified: 2014-11-17
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.
0
Comment
Question by:Pinpindesbois
  • 9
  • 7
  • 4
20 Comments
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 40443429
Import-Csv C:\Users\me\username.csv | foreach{Set-ADUser -identity $_-SamAccountName  $_.sAMAccountName.tolower()}

Open in new window


You have a - instead of a . listed in this code should be.

Import-Csv C:\Users\me\username.csv | foreach{Set-ADUser -identity $_.SamAccountName  $_.sAMAccountName.tolower()}

Open in new window

0
 

Author Comment

by:Pinpindesbois
ID: 40443499
No, always the error :
Set-ADUser : Impossible de trouver un paramètre positionnel acceptant l'argumen
t « me ».
Au niveau de ligne : 1 Caractère : 74
+ Import-Csv C:\Users\me\username.csv | foreach{Set-ADUser <<<
<  -identity $_.SamAccountName  $_.sAMAccountName.tolower()}
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBind
   ingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDire
   ctory.Management.Commands.SetADUser

Set-ADUser : Impossible de trouver un paramètre positionnel acceptant l'argumen
t « t.desk ».
Au niveau de ligne : 1 Caractère : 74
+ Import-Csv C:\Users\me\username.csv | foreach{Set-ADUser <<<
<  -identity $_.SamAccountName  $_.sAMAccountName.tolower()}
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBind
   ingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDire
   ctory.Management.Commands.SetADUser

Open in new window


In english: A positional parameter cannot be found that accepts argument "me".

I want to be more precise: I want import the samaccountname contained in the csv file, in powershell and for EACH name, apply the function Lowercase to the SamAccountName. And i want to see the result in the AD console when i right click-Properties-Account on the user.
Here my list in the CSV :
"sAMaccountname"
"t.DESK2"
"t.Desk"

Open in new window

0
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 40443543
First get rid of the " in your CSV file. I have done imports many times and never used quotes in my csv file.

Try this

Import-Csv C:\Users\me\username.csv | foreach {
$samid= $_.samaccountname
$samid=$samid.tolower()
Set-ADUser -identity $_.samaccoutname -samaccoutname $samid
}
0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 

Author Comment

by:Pinpindesbois
ID: 40443584
I tried, but i have this error now :
Set-ADUser : Impossible de valider l'argument sur le paramètre « Identity ». L'
argument est null. Indiquez un argument non-null et réessayez.
Au niveau de C:\Users\me\dc.ps1 : 4 Caractère : 21
+ Set-ADUser -identity <<<<  $_.samaccoutname -samaccoutname $samid
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

Set-ADUser : Impossible de valider l'argument sur le paramètre « Identity ». L'
argument est null. Indiquez un argument non-null et réessayez.
Au niveau de C:\Users\me\dc.ps1 : 4 Caractère : 21
+ Set-ADUser -identity <<<<  $_.samaccoutname -samaccoutname $samid
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

Open in new window


Translation : impossible to validate an argument on a parameter: identity.
Mhh now i have a doubt about the command: Set-ADUser -identity $_.samaccoutname -samaccoutname $samid.


PS: if i have the " in my csv it's because i opened it with the text editor. Or not? (on my server i can't read csv for the moment.)
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40443611
samaccoutname should be samaccountname. It is misspelled.

Import-Csv C:\Users\me\username.csv | foreach {
$samid = $($_.samaccountname).tolower()
Set-ADUser -identity $_.samaccountname -samaccountname $samid
}

Open in new window

0
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 40443641
Good catch. Sorry about that.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40443645
It happens to the best of us!
0
 

Author Comment

by:Pinpindesbois
ID: 40443664
OK really thank you, it's working... Or not i have not the result i looking for. With screenshot;  screenshotAs you can see the SAMACCOUNTNAME is in lowercase as i asked =).
But in AD: screenshot
Why it's always t-DESK?

Use another commande like Rename instead "-samaccountname"?

Thank you a lot btw now i understand few more things in powershell.
0
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 40443673
How many DCs do you have? The info could be changed and may need time to replicate if you have multiple DCs.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40443683
Joseph has a point. Depending on your replication schedule it could take a bit.
0
 

Author Comment

by:Pinpindesbois
ID: 40443698
Only two DC. Are you sure this command change the parameter in AD? Cause i always suceed today to made the change in powershell and i got the same problem.
The replication took only 5-10 minutes. I can wait.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40443707
it may be because of the UserPrincipalName as well
0
 

Author Comment

by:Pinpindesbois
ID: 40443713
So i need to change the UPN too? What's the command -UserprincipalName?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40443715
-UserPrincipalName

Import-Csv C:\Users\me\username.csv | foreach {
$samid = $($_.samaccountname).tolower()
Set-ADUser -identity $_.samaccountname -samaccountname $samid -UserPrincipalName "$($samid)@contoso.com"
}
0
 

Author Comment

by:Pinpindesbois
ID: 40443728
Ok ,i did it but no change.
As you can see, when i rename on the AD the user t.desk i have this windows. And this is ALWAYS in t.DESK.regc.jpg
0
 

Author Comment

by:Pinpindesbois
ID: 40443732
Is this another command like SET-ADOBJECT?
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 2000 total points
ID: 40443747
Try this

Import-Csv C:\Users\me\username.csv | foreach {
$samidoriginal = $($_.samaccountname).tolower()
$samid = $($_.samaccountname).tolower()
$samid = $samid + "1"
Set-ADUser -identity $_.samaccountname -samaccountname $samid -UserPrincipalName "$($samid)@contoso.com"
Set-ADUser -identity $samid -samaccountname $samidoriginal -UserPrincipalName "$($samidoriginal)@contoso.com"
}

Open in new window


There might be a better way but it should work.
0
 

Author Comment

by:Pinpindesbois
ID: 40443751
Wow nice, it work!! But, didn't understand your code yet. I will work on it. Just why $samid +"1" ? And the last line, what did it change in the script?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40443764
All it does is change the name to
t.desk1 then changes it back to t.desk.

It seems ADUC doesn't update if the case changes unless you completely rename it first
0
 

Author Comment

by:Pinpindesbois
ID: 40443781
Ah i understand now, strange.
Thank you!
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 walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Welcome to 2018! Exciting things lie ahead in the world of tech. To start things off, we compiled great member articles on how to stay safe, ways to learn, and much more! Read on to start your new year right.
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 anti-spam), the admin…
Screencast - Getting to Know the Pipeline

621 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