Active Directory – Change Profile Path & Group member

This litte script changes the profile path of a specified OU of AD Users that are in a specific group.


Import-Module ActiveDirectory

$ou = "ou=YOUROU,dc=ad,dc=uclv,dc=net"

$properties = "MemberOf"
$i = 0
Get-ADUser -Filter * -SearchBase $ou -Properties $properties | Where-Object { $_.MemberOf -like "*GROUPNAME*"} |

ForEach-Object {

Write-Host "User:  "$_.SamAccountName -ForegroundColor Yellow
Write-Host "Count: "$i -ForegroundColor Yellow

$ProfilePath = "\\profiles.bewi.at\{0}$\roaming" -f $_.SamAccountName
Write-Host "Path:  "$ProfilePath -ForegroundColor Yellow
Write-Host "-----"
$i++
# Set profile path
$result_setprofile = Set-ADUser $_.samaccountname -ProfilePath $ProfilePath
if ($?)
{
Write-Host "Profile set for user:"$_.samaccountname -ForegroundColor Green
}
# Set correct groups
Remove-ADGroupMember cluster-migration-roaming $_.samaccountname -Confirm:$false
Add-ADGroupMember cluster-migration-new $_.samaccountname
if ($?)
{
Write-Host "Groups set for user: "$_.samaccountname -ForegroundColor Green
}
}