Moving users

I’ve got myself into a situation and am walking away for the night. I’m hoping you guys can give me some ideas. I have a csv with users who should be active:

samAccountName BuildingID
123456 BuildingA
654321 BuildingB

I’ve got a hash to point these users to the correct OU when they get created:

$bldOU = @{ "BuildingA" = "OU=BuildingA,OU=staff,DC=contoso,DC=local" "BuildingB" = "OU=BuildingB,OU=staff,DC=contoso,DC=local" } 

I’ve been able to use this when I’m creating new users however I have a bunch of existing users in my “DisabledUsers” OU who need to be moved back to their correct OU based off this information. I am currently trying to do something like this but it’s not working and I’m stuck:

$csv = import-csv "\serverusers.csv" $user = Get-ADUser $csv.samAccountName -Properties CanonicalName $userOU = ($user.DistinguishedName -split ",",2)[1] foreach ($user in $csv | where {$userOU -ne $bldOU[$user.buildingID]}){ $2bMoved = Get-ADUser -Identity $user.samAccountName Move-ADObject -Identity $2bMoved.DistinguishedName -TargetPath $bldOU[$user.buildingID] } 

submitted by /u/gaz2600
[link] [comments]

Leave a Reply