Powershell to populate the Office attribute for users with the containing OU name

Most companies will name OUs after the offices that users are located in, simple script I wrote to add the containing OU name to the office attribute

$aduser = Get-ADUser -SearchBase ‘The OU you want to do this to’ -Filter *

foreach ($i in $aduser)
{
$distinguishedName = $i.DistinguishedName
$OU = [regex]::match($distinguishedName,'(?=OU)(.*\n?)(?<=.)').Value
$OU = $OU.Substring(3)

$OU = $OU.split(',')[0]

echo " Processing $OU"
Set-ADUser -Identity $i -Office $OU

}

Hope this helps someone :)

Comments

Popular posts from this blog