Sunday, November 18, 2012

Powershell - Bulk change home directory for AD users


#Change home directory
# must be logged on with administrative permission for AD
$ou = "TestOU"
$Search = New-Object DirectoryServices.DirectorySearcher([ADSI]"LDAP://OU=$ou,DC=USA,DC=DOMAIN,DC=COM")
$Search.filter = "(&(objectCategory=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))" #users, not disabled
Foreach($result in $Search.Findall()){
$user = $result.GetDirectoryEntry()
$updHome = $($user.HomeDirectory)
#$new = $updHome.ToLower().Replace("\\priricfs01\users\","\\pricifs\ricusers\")
$new = $updHome.ToLower().Replace("\\pricifs\ricusers\","\\priricfs01\users\")
$new = $new.Replace("(","")
$new = $new.Replace(")","")
$objUser = [ADSI]"$($User.Path)" # Get user object
$objUser.put("HomeDirectory", "$new") # Make change
$objUser.SetInfo() # Commit change
}

No comments:

Post a Comment