powershell scrpit to populate user principal name from a list of users

I have  a list of users  in a CSV that is the SanAccountName. I need to run a powershell script to populate the LogOnName Portion of the UPN. here is what I have so far that If I use a specific username it works perfectly. I am just having issues calling on the list from the CSV file. any help would be great. thanks.

Import-Module ActiveDirectory

$UserList = Import-csv C:\temp\aduserUPNList.csv

foreach ($user in $userlist) {

get-aduser -SamAccountName $user | foreach {set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “@ci.fort-worth.tx.us”)} }


Error message:
Get-ADUser : A parameter cannot be found that matches parameter name 'SamAccountName'.
At line:7 char:12
+ get-aduser -SamAccountName $user | foreach {set-aduser -identity $_.SAMAccountNa ...
+            ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
TXRhinoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
Try..
Import-csv C:\temp\aduserUPNList.csv | %{
Set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “@ci.fort-worth.tx.us”)
}

Open in new window

Input csv file should be like..
SAMAccountName 
UserA
UserB
UserC

Open in new window

0
TXRhinoAuthor Commented:
I tried that and ensured the csv file looked correct as well with no success.. here is the error I receive.

Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At line:2 char:22
+ Set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “ ...
+                      ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
SubsunCommented:
The argument is null. Supply a non-null argument
The error says the value for $_.SAMAccountName is $null.. mostly the problem with input file.. Following code will ignore the empty values/lines in the input file..
Import-csv C:\temp\aduserUPNList.csv } ?{$_.SAMAccountName} | %{
Set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “@ci.fort-worth.tx.us”)
}

Open in new window

If it is not working please post your sample input file..
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

TXRhinoAuthor Commented:
I double checked and the input file is just what was described in your original post. ran it again and this is what I get...

At line:3 char:38
+ Import-csv C:\temp\aduserUPNList.csv } ?{$_.SAMAccountName} | %{
+                                      ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
0
TXRhinoAuthor Commented:
Captureof-CSV.JPG
0
SubsunCommented:
There was a typo in last comment..
Import-csv C:\temp\aduserUPNList.csv | ?{$_.SAMAccountName} | %{
Set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “@ci.fort-worth.tx.us”)
}

Open in new window

If it is not working just create a simple input file with two three users put the SAMAccountName's in a text file and try the following code..
GC C:\temp\aduserUPNList.txt | ?{$_} | %{
Set-aduser -identity $_ -UserPrincipalName ($_ + “@ci.fort-worth.tx.us”)
}

Open in new window

Text file..
UserA
UserB
UserC

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SubsunCommented:
Check the header name in your csv.. there snould not be any trailing space after SAMAccountName.

Example "SAMAccountName " or "SAMAccountName   " which will give you error..
0
TXRhinoAuthor Commented:
The text file was the trick... Many Thanks.

Ryan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.