Use PowerShell to change user roaming profile locations

Here’s a script I used recently to change users’ roaming profile paths from one server (\\OLDSERVER) to another (\\NEWSERVER). It identifies the old server by comparing strings with a search term and only changes those. It also doesn’t just fire and forget – it stops every 5 records to confirm you wish to continue.


$RootDN = [ADSI] ''

$Searcher = New-Object System.DirectoryServices.DirectorySearcher($RootDN)

$Sorter = New-Object System.DirectoryServices.SortOption
$Sorter.PropertyName = "sAMAccountName"

$Searcher.Filter = "(&(objectCategory=person)(objectClass=user))"
$Searcher.Sort = $Sorter

$Users = $Searcher.FindAll()

Write-Host "There are" $Users.Count "users in the Active Directory"

$i = 0

ForEach ($User in $Users) {

# Fail safe code. Prompt every 5 records.

If (($i % 5) -eq 0) {

Write-Host
Write-Host "I have processed 5 records... should I continue?"
Write-Host "To Cancel, press Ctrl+Break"

While ($Response -ne "Y") {
$Response = Read-Host "Type Y and press [Enter] to continue."
}

Write-Host

}

$UserDN = [ADSI]$User.Path

$UserprofilePath = $UserDN.profilePath.ToString()
$UserAccountName = $UserDN.sAMAccountName.ToString()

If ($UserprofilePath.ToLower().Contains('\\OLDSERVER\path'.ToLower())) {

Write-Host $UserDN.sAMAccountName","$UserDN.displayName","$UserDN.profilePath

$NewProfilePath = $UserDN.profilePath.ToString().ToLower()
$NewProfilePath = $NewProfilePath.Replace("OLDSERVER\path", "NEWSERVER\path")

Write-Host $NewProfilePath
Write-Host

# NB: Only uncomment the lines below when you are absolutely
# sure you wish to run the script.
#$UserDN.Put("profilePath", "$NewProfilePath")
#$UserDN.SetInfo()

}

$Response = ""
$i++
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>