Active Directory - Copy OrganizationalUnit name to user Location attribute

84 views Asked by At

Is there a way to programmatically copy the OrganizationalUnit name of an AD user to it's location attribute?

I have several OUs on my AD tree where each of them is related to a physical department of my company. Users were created only with the name attribute to speed up things, but now I need to fulfil their information, and I'm thinking a way of scripting this.

I've tried to get all users properties from Get-ADUser -Filter * -Properties *, but the OU only shows up under DistinguishedName, and I have no idea how to copy this property to the user location attribute, filtering out only the OU name (and, if I have sub OUs, group them as OU\subOU).

1

There are 1 answers

1
Sergio Baiao On
$USERS = Get-ADUser -Filter * -Properties CanonicalName
foreach($user in $USERS)
{
$copy = (Split-Path $user.CanonicalName -Parent) -replace 'domain.name\\' 
Set-ADUser $user -Replace @{Department=$copy}
}