0

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 fulfill 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 Answers1

0

Answering my own question after a lot of research, trial and error:

$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}
}