Set Department

Hi Everyone.
I am working on a PowerShell script to update user's department field. I have a CSV file that has the username and department field. i want to set the department of the users in the CSV based on what is in the CSV.  the script below is not working for me. any idea ?

Import-Module ActiveDirectory
$Users = Import-csv c:\Users.csv
foreach($User in $Users){
Set-ADUser $User.username -Department $User.Department
Debrata 1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Should work just fine. Do you get error messages, and if so, which ones?
Does your csv have a header line with a column named "UserName" and one named "Department"?
Is the UserName the user's SamAccountName attribute?
Do you have enough permissions to change the department? In particular: are you running this in an elevated console?

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
Debrata 1Author Commented:
Thanks! it's working. I just had to change the column names as you pointed out.
Question answered.
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

From novice to tech pro — start learning today.