Bulk Attribute Change of Users in Active Directory
Prerequisites: ActiveRoles Management Shell for Active Directory must be installed on the host running this script.
As mentioned in my post “Detect Orphaned HomeDirectories and Roaming Profiles” I was given the task to migrate data from one file server to the other. Since the paths to homedirectories and roaming profiles would change I needed to come up with a solution to change the paths of hundreds of users. Of course the solution would be a powershell script.
First I want to load the snappin so I don’t have to open the ActiveRoles Management Shell everytime I want to run the script.
Add-PSSnapin -name Quest.ActiveRoles.ADManagement
Next I want to fetch the users from Active Directory. This is done with the ‘get-QADUser’ cmdlet.
$users = Get-QADuser -SearchRoot 'mydomain.local/Users' -SizeLimit 0 | Sort-Object Name
This will return an array of all the users in the searchroot (it also searches in subcontainers). I’m piping the result to the ‘Sort-Object’ cmdlet as I want to sort the array by the names of the users.
Now that the users have been fetched from Active Directory all that is left to do is to run through the array, change the different paths, and commit the changes to Active Directory.
for( $i = 0; $i -lt $users.length; $i++ )
{
$oldHomeDirectory = ""
$oldProfilePath = ""
$oldTsHomeDirectory = ""
$oldTsProfilePath = ""
$user = $users[$i]
if( $user.HomeDirectory -ne $null ) # only if HomeDirectory is filled in
{
$oldHomeDirectory = $user.HomeDirectory
$user.HomeDirectory = $user.HomeDirectory -replace "\\\\server01\\homedirectories\$", \\server02\homedirectories$
}
if( $user.ProfilePath -ne $null ) # only if ProfilePath is filled in
{
$oldProfilePath = $user.ProfilePath
$user.ProfilePath = $user.ProfilePath -replace "\\\\server01\\profiles\$", \\server02\profiles$
}
if( $user.TsHomeDirectory -ne $null ) # only if TsHomeDirectory is filled in
{
$oldTsHomeDirectory = $user.TsHomeDirectory
$user.TsHomeDirectory = $user.TsHomeDirectory -replace "\\\\server01\\homedirectories\$", \\server02\homedirectories$
}
if( $user.TsProfilePath -ne $null ) # only if TsProfilePath is filled in
{
$oldTsProfilePath = $user.TsProfilePath
$user.TsProfilePath = $user.TsProfilePath -replace "\\\\server01\\tsprofiles\$", \\server02\tsprofiles$
}
...
}
The loop runs through the array and does a ‘search and replace’ on the user attributes I want to change. If you want to change anything else, here’s where you want to add it. Just type “get-QADUser -?” to see all the attributes you can change. Also I’m saving the paths in a new set of variables before I change them. The only reason for this is that I want to use them for output e.g. to a log file. The last thing to do is simply to commit the changes and write some output lines for logging purposes.
$user.commitchanges()
$user.DisplayName + " (" + $user.sAMAccountName + ") updated"
"HomeDir:`t" + $oldHomeDirectory + " >> " + $user.HomeDirectory
"ProfilePath:`t" + $oldProfilePath + " >> " + $user.ProfilePath
"TsHomeDir:`t" + $oldTsHomeDirectory + " >> " + $user.TsHomeDirectory
"TsProfilePath:`t" + $oldTsProfilePath + " >> " + $user.TsProfilePath
"----------------------------------------------"
Here’s the entire script: Bulk Attribute Change of Users in Active Directory
No comments yet.