# encoding: ascii
# api: powershell
# title: Update User Profiles
# description: This is an update to the original script from TheMoblin http://poshcode.org/4651 for larger implementations for larger AD infrastructure. I have help with this from Don and Mike at Powershell.org forums.
# version: 0.1
# type: module
# author: xspader
# license: CC0
# x-poshcode-id: 5366
# x-archived: 2015-06-23T00:26:15
# x-published: 2015-08-13T23:24:00
#
# This allows for only the users that need to be updated to be actioned, and also allows for targetting to a specific OU. This was updated to cope with a rather flat OU structure with thousands of user objects in each OU when only a few hundred may need updating
#
$OldServer = 'OldServer'
$NewServer = 'NewServer'
 
Import-Module ActiveDirectory
Get-ADUser -Filter "Enabled -eq 'true' -and HomeDirectory -like '*$OldServer*' -or ProfilePath -like '*$OldServer*'" -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx" -Properties HomeDirectory, ProfilePath |
ForEach-Object {
    if ($_.HomeDirectory -like "*$OldServer*") {
        Set-ADUser -Identity $_.DistinguishedName -HomeDirectory $($_.HomeDirectory -replace $OldServer, $NewServer)
    }
    if ($_.ProfilePath -like "*$OldServer*") {
        Set-ADUser -Identity $_.DistinguishedName -ProfilePath $($_.ProfilePath -replace $OldServer, $NewServer)
    }
}
