0
votes

I'm new to AD - Automation via Powershell and although I managed to do Bulk Modify/Update the Location of our AD users based in our OU for some reason I am having difficulty replicating the script for the OU based in our on site office.

I hope i'm making sense here but probably this will help you answer or help me. Here is my script:

Import-Module ActiveDirectory  

$Users = Import-Csv -Delimiter "," -Path "c:\scripts\van.csv"

Get-ADUser -Filter 'DisplayName -like "*"' | Set-ADUser -Replace @{PhysicalDeliveryOfficeName="Van Nuys"}

When I ran this script on Powershell what happens is it changes the location of the user's based in our OU not based on Van Nuys. The csv file that I have has DisplayName, Title and Office with the corresponding information of each users. But I don't know why it's changing the users in our OU.

I hope you can help me.

Sincerely, LP

1
You're not using your $users variable anywhere... You're grabbing every user in your AD and setting their office location. - Nick
What should be my script for me to update the attributes for the users that are only listed on the csv file? - Larry Paul

1 Answers

0
votes

Assuming that your CSV has the sAMAccountName, and you want all users updated to Van Nuys, something like this will work.

Import-Module ActiveDirectory  

$Users = Import-Csv -Delimiter "," -Path "c:\scripts\van.csv"

foreach($user in $users)
{
 Set-ADUser -Identity $user.samaccountname -Replace @{PhysicalDeliveryOfficeName="Van Nuys"}
}

If you really only have their display name, you can TRY modifying it to this, but since it's not a unique identifier you'll be really lucky if it works.

foreach($user in $users)
{
 Get-aduser -filter {DisplayName -like $user.Displayname} |Set-ADUser -Replace @{PhysicalDeliveryOfficeName="Van Nuys"}
}