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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
$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++ } |
Â