How to edit only the Firstname (givenName) of multiple users and import with csv

Clash Royale CLAN TAG#URR8PPPHow to edit only the Firstname (givenName) of multiple users and import with csv
If I have a list of users where the samAccountName for all the users are all numerical Example, 745744, 745746, etc…. I was looking for a powershell script that could accomplish the following:
Is having only one column in excel with the samAccountName sufficient for the csv, or do i need to also have all of the users givenName as well inside of the csv?? Example column A samAccountName, column B Firstname??
Example givenName is, JOAQUIN, I would need the first name to be XXJOAQUIN. I have about 500 users that I would have to do this for, and we are doing it manually right now, So the only thing that I need changed is adding the “XX” before the users givenNames….
Thanks everyone in advance.
3 Answers
3
If your CSV file just contains a header with the samAccountName followed by the samAccountNames like this:
samAccountName
samAccountName
745744
745746
Then you can use the following piece of code, change the path to the path of the csv file with your users.
This could be made shorter and use the pipeline better to make it less code, but this will work just fine.
$users = Import-Csv -Path .users.csv
foreach($user in $users) {
$obj = Get-ADUser -Identity $user.samAccountName
Set-ADUser -Identity $user.samAccountName -GivenName ("xx" + $obj.GivenName)
}
OK i ran the script and it works perfect. Last question if it was you and you had 500 users to do would u do all of them at one time or would you split it up in batches?
– jwalk_pv2001
35 mins ago
You could do all of them at the same time, no problem. Since it gets each user by samAccountName it doesn't really matter which OU they are in. The only reason to do it in batches would be if it makes sense to you, like if you need to inform the users or there are other processes in your end where you need to do something. If not, go nuts and do it in a single go, that is after all the awesome power of the shell :)
– Henrik Stanley Mortensen
16 mins ago
sooo Amazing u are!!!! Henrik you should take a peek at the script below from Paxz when i ran his script on a test user just now it replaced the entire givenname with only xx. I wonder what he did wrong??
– jwalk_pv2001
9 mins ago
If you have a CSV with samAccountName as the first column, you can do something like this.
$Users = Import-CSV "Users.csv"
Foreach($User in $Users){
$ADUser = Get-ADUser $User.samAccountName
$ADUser.GivenName = "XX$($ADUser.GivenName)"
Set-ADUser -Instance $ADUser
}
If you are searching for numerical users, you can look up in the documentation to use Get-ADUser to get all your users, and use a foreach loop to evaluate if it is numerical, and set the account.
Get-ADUser
foreach
is it not redundant to pipe your $Users variable into a foreach? You could remove the
$Users | and get the same result :)– Henrik Stanley Mortensen
1 hour ago
$Users |
Oh yeah, (edited), I usually pipe things into a
Foreach-Object but decided to use a traditional foreach statement to make things clearer to understand, and forgot to remove the pipe ;-)– HAL9256
1 hour ago
Foreach-Object
foreach
You can also pipe the output directly from Get-ADUser to Set-ADUser like this:
Get-ADUser
Set-ADUser
$users = Import-Csv -Path .users.csv
foreach($user in $users) {
Get-ADUser -Identity $user.SamAccountName | % {Set-ADUser -Identity $_.SamAccountName -GivenName ("xx" + $_.GivenName)}
}
But since you are reffering to the same element twice, I don't see a reason why you shouldn't do it directly like this:
$users = Import-Csv -Path .users.csv
foreach($user in $users) {
Set-ADUser -Identity $user.SamAccountName -GivenName ("xx" + $_.GivenName)
}
this one didnt work It totally replaced the givenname with only xx. Henrik Stanley worked as intended!!
– jwalk_pv2001
12 mins ago
@jwalk_pv2001 which one of both didnt work?
– Paxz
9 mins ago
@Paxz I think that the issue is that the $_. doesn't work since the object from the Get-ADUser is for some reason not in the pipeline. I am not sure why, I tested this myself first, couldn't get it to work and decided to go with getting the ad user in a variable first. I think if we run the command with a -verbose it should be possible to see what gets passed in the pipe.
– Henrik Stanley Mortensen
6 mins ago
@HenrikStanleyMortensen Well using foreach like in the edit fixes it. (not the optimal soluton)
– Paxz
5 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
hey henrik and hal9256. thanks for the super quick response. You guys are AWESOME. All of the samAccount names are numerical (they are all student accounts). Im going to test the script out in a few minutes on a test user to see what it does. i will reply back shortly.. Question if the path to the csv was c:users.csv do i edit the script to look like this $users = Import-Csv -Path c:users.csv ? do i have to enclose quotes around the path?? sorry im not smart lol
– jwalk_pv2001
53 mins ago