\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!\nUse a descriptive subject. Don't say \"Need help\" or \"PowerShell Help\", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n\n <\/p>","upvoteCount":0,"datePublished":"2019-04-26T13:19:50.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Well, if the snippet you have works, you just need to expand the ‘get-aduser’ from a single user, to all users.<\/p>\n
Get-ADUser 'userid'\n\n#to\n\nGet-ADUser -Filter *\n<\/code><\/pre>\nYou want to thoroughly test this before letting it loose on your entire AD.<\/p>","upvoteCount":0,"datePublished":"2019-04-26T13:21:10.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Yes this script works but I would have to enter each userid/samaccountname (get-aduser ‘samaccountname’) for each users.<\/p>\n
I have a CSV that I want to pull the user from. It has one value - the samaccountname and I want it to replace the UPN prefix with the samaccountname. I am not a programmer and learn as I go. Here are some things I have tried.<\/p>\n
$Users = Import-Csv -path C:\\temp\\InputTest1.csv<\/p>\n
Get-ADUser $Users | \nForEach-Object{ \n$upn = $.UserPrincipalName -replace ‘.*@’, ($<\/em>.SamAccountName + ‘@’) \n$_ | Set-ADUser -UserPrincipalName $upn \n}<\/p>\nor<\/p>\n
$Users = Import-Csv -path C:\\temp\\InputTest1.csv<\/p>\n
Foreach($User in $Users) {<\/p>\n
Get-aduser -identity $User | Set-ADUser -UserPrincipalName $User<\/p>\n
}<\/p>\n
or<\/p>\n
$Users = Import-Csv -path C:\\temp\\InputTest1.csv<\/p>\n
Get-ADUser $Users | \nForEach-Object{ \n$upn = $.UserPrincipalName -replace ‘.*@’, ($<\/em>.SamAccountName + ‘@’) \n$_ | Set-ADUser -UserPrincipalName $upn \n}<\/p>","upvoteCount":0,"datePublished":"2019-04-26T13:22:32.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/5","author":{"@type":"Person","name":"spiceuser-vzcws","url":"https://community.spiceworks.com/u/spiceuser-vzcws"}},{"@type":"Answer","text":"\n\n
<\/div>\n
ErikSanchez:<\/div>\n
\nI need a script that will read the SAMAccountname from a CSV and update the UPN in each user account with the SAMAccountname.<\/p>\n
Here are some things I have tried.<\/p>\n<\/blockquote>\n<\/aside>\n
Some of them seem promising, what were the issues you run into?<\/p>","upvoteCount":0,"datePublished":"2019-04-26T13:24:39.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Will do - Thank you for the info!<\/p>","upvoteCount":0,"datePublished":"2019-04-26T13:25:51.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/7","author":{"@type":"Person","name":"spiceuser-vzcws","url":"https://community.spiceworks.com/u/spiceuser-vzcws"}},{"@type":"Answer","text":"
The main issue was on the Get-ADUser – Cannot validate argument on parameter ‘Identity’. The argument is null…<\/p>\n
This seems to me like it is not reading the CSV…<\/p>","upvoteCount":0,"datePublished":"2019-04-26T13:27:18.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/8","author":{"@type":"Person","name":"spiceuser-vzcws","url":"https://community.spiceworks.com/u/spiceuser-vzcws"}},{"@type":"Answer","text":"\n\n
<\/div>\n
ErikSanchez:<\/div>\n
\nThe main issue was on the Get-ADUser – Cannot validate argument on parameter ‘Identity’. The argument is null…<\/p>\n
This seems to me like it is not reading the CSV…<\/p>\n<\/blockquote>\n<\/aside>\n
Does your CSV have headers?<\/p>\n
e.g.<\/p>\n
UserNameList\nusername01\nusername02\nusername03\n<\/code><\/pre>\n$csv = import-csv \"c:\\file.csv\"\n\nforeach($line in $csv){\nGet-ADUser $line.UserNameList |\n ForEach-Object{\n $upn = $_.UserPrincipalName -replace '.*@', ($_.SamAccountName + '@')\n $_ | Set-ADUser $line.UserNameList -UserPrincipalName $upn\n }\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-04-26T13:35:05.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/9","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"That is getting closer, but still an error:<\/p>\n
Set-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input \nand its properties do not match any of the parameters that take pipeline input. \nAt line:7 char:10<\/p>\n
\n$_ | Set-ADUser $line.UserNameList -UserPrincipalName $upn<\/li>\n \n<\/code><\/pre>\n<\/li>\nCategoryInfo : InvalidArgument: (CN=USER<\/em>…=DOMAIN<\/em>,DC=local:PSObject) [Set-ADUser], ParameterBindingException<\/li>\nFullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.SetADUser<\/li>\n<\/ul>","upvoteCount":0,"datePublished":"2019-04-26T13:47:30.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/10","author":{"@type":"Person","name":"spiceuser-vzcws","url":"https://community.spiceworks.com/u/spiceuser-vzcws"}},{"@type":"Answer","text":"\n\n
<\/div>\n
ErikSanchez:<\/div>\n
\nThat is getting closer, but still an error:<\/p>\n
Set-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input \nand its properties do not match any of the parameters that take pipeline input. \nAt line:7 char:10<\/p>\n
\n$_ | Set-ADUser $line.UserNameList -UserPrincipalName $upn<\/li>\n \n<\/code><\/pre>\n<\/li>\nCategoryInfo : InvalidArgument: (CN=USER<\/em>…=DOMAIN<\/em>,DC=local:PSObject) [Set-ADUser], ParameterBindingException<\/li>\nFullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.SetADUser<\/li>\n<\/ul>\n<\/blockquote>\n<\/aside>\ncan you post your code? What I posted still needs to be updated, that will not work just like that. Look at the $upn part, you see a ‘$_’ there, that needs to be changed to $line and so forth.<\/p>","upvoteCount":0,"datePublished":"2019-04-26T13:48:12.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/11","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
$csv = import-csv \"C:\\temp\\InputTest.csv\"\n\nforeach($line in $csv){\nGet-ADUser $line.UserNameList |\n ForEach-Object{\n $upn = $_.UserPrincipalName -replace '.*@', ($_.SamAccountName + '@')\n $_ | Set-ADUser $line.UserNameList -UserPrincipalName $upn\n }\n}\n<\/code><\/pre>\nWithin the CSV I added the header UserNameList…<\/p>\n
After I got the error I changed the code to just the following and it listed each test account I had in the CSV file. So the import works.<\/p>\n
$csv = import-csv \"C:\\temp\\InputTest.csv\"\n\nforeach($line in $csv){\nGet-ADUser $line.UserNameList}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-04-26T13:58:31.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/12","author":{"@type":"Person","name":"spiceuser-vzcws","url":"https://community.spiceworks.com/u/spiceuser-vzcws"}},{"@type":"Answer","text":"ok, cool, that was just an example though as we don’t know what your actual CSV looks like…<\/p>\n
Do you have more than one column in it?<\/p>\n
$csv = import-csv \"C:\\temp\\InputTest.csv\"\n\nforeach($line in $csv){\nGet-ADUser $line.UserNameList |\n ForEach-Object{\n $upn = $_.UserPrincipalName -replace '.*@', ($_.SamAccountName + '@')\n $_ | Set-ADUser $line.UserNameList -UserPrincipalName $upn\n }\n}\n<\/code><\/pre>\nneeds to be updated to refext the foreach, rather than a foreach-object<\/p>\n
$csv = import-csv \"C:\\temp\\InputTest.csv\"\n\nforeach($line in $csv){\nGet-ADUser $line.UserNameList |\n ForEach-Object{\n $upn = $line.UserPrincipalName -replace '.*@', ($line.SamAccountName + '@')\n Set-ADUser $line.UserNameList -UserPrincipalName $upn\n }\n}\n<\/code><\/pre>\nmore like so.<\/p>\n
again, i can not test that as I don’t know what your CSV looks like.<\/p>","upvoteCount":0,"datePublished":"2019-04-26T14:03:07.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/13","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
That didn’t work either…<\/p>\n
Here is what is in my csv<\/p>\n
UserNameList eriktest etest2 eriktest3 RQITest<\/p>\n
<\/p>","upvoteCount":0,"datePublished":"2019-04-26T14:14:42.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/14","author":{"@type":"Person","name":"spiceuser-vzcws","url":"https://community.spiceworks.com/u/spiceuser-vzcws"}},{"@type":"Answer","text":"
if I understand you want to match samaccountname as userprinipal name,<\/p>\n
$csv = import-csv \"c:\\file.csv\"\nforeach($line in $csv){\n $upn='@domain.com'\n Set-ADUser $line.UserNameList -UserPrincipalName {$_.$line.UserNameList + $upn }\n \n}\n<\/code><\/pre>\n^ change domain.com<\/a> to real user principal name<\/p>","upvoteCount":0,"datePublished":"2019-04-26T14:22:56.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/15","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"To clarify… Some of our users have for their UPN: [email protected] and SAM as: Domain\\ #######. What I want to do is read from a CSV that has the SAMAccountName to identify the correct users and copy the SAMAccountname and place it in the Prefix of the UPN. I do not want to mess with anything after the @…<\/p>\n
New UPN would be ######@domain.local<\/span> to match the SAMAccountname…<\/p>","upvoteCount":0,"datePublished":"2019-04-26T14:45:06.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/16","author":{"@type":"Person","name":"spiceuser-vzcws","url":"https://community.spiceworks.com/u/spiceuser-vzcws"}},{"@type":"Answer","text":"like so?<\/p>\n
foreach($line in $csv){\n $upn = ((get-aduser $line.usernamelist).userprincipalname -split \"@\")[1]\n set-aduser $line.usernamelist -UserPrincipalName \"$($line.usernamelist)@$upn\"\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-04-26T15:48:13.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/17","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"\n\n
<\/div>\n
ErikSanchez:<\/div>\n
\nTo clarify… Some of our users have for their UPN: [email protected] and SAM as: Domain\\ #######. What I want to do is read from a CSV that has the SAMAccountName to identify the correct users and copy the SAMAccountname and place it in the Prefix of the UPN. I do not want to mess with anything after the @…<\/p>\n
New UPN would be ######@domain.local<\/span> to match the SAMAccountname…<\/p>\n<\/blockquote>\n<\/aside>\nNo real need to use a CSV and mess with the CSV at all really… The CSV might be complicating it, you could query the AD with a simple filter and Get-aduser for Samaccount Not like Userprincipalname and then pass it to a Foreach Loop to Set Userprincipalname like Samaccount.<\/p>\n
I’ve found when dealing with UPN’s that if you change it via powershell and don’t specify an @domain.local<\/span> then it blanks out that field.<\/p>","upvoteCount":0,"datePublished":"2019-04-26T17:39:28.000Z","url":"https://community.spiceworks.com/t/update-upn-with-samaccountname/709279/18","author":{"@type":"Person","name":"cweb","url":"https://community.spiceworks.com/u/cweb"}}]}}
Neally
(Neally)
April 26, 2019, 1:19pm
3
Welcome
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don't say "Need help" or "PowerShell Help", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…