Powershell – Using Powershell to bulk change the Home folder path of AD objects – %username”

active-directoryhome-directorypowershell

I have done a lot of searching on this, and still cannot find the answer.

I have created a script which will bulk move all user objects in one OU to another OU. This works fine. Now I am trying to bulk change those users' Home Folder paths (and drive letter). So far, I can get the script to change the path/drive letter correctly, but I cannot get it to add the user's logon name to the end of the path (\server\share\username). The users' Home folders are named after their logon names. Normally, we would do this on an individual basis by just putting \server\share\%username% in the Home folder path in AD, but %username% does not seem to be an option with Powershell. If I add "%username%" to the end of the path, the AD path will look like this – \server\share\%username% (the user's name appears as "%username%", not their actual logon name).
I have tried a number of ways to get this to work (variables, $_.SamAccountName, etc), but so far, no luck.

Here's my script, so far…

#This script will move objects from one OU to another and set AD Home folder path.

Clear-Host
write-host "This script will move objects from one OU to another and set AD Home folder path." -ForegroundColor Magenta

start-sleep -s 2
#Global Variables
$global:path = "OU=users,OU=IT,"dc=contoso,dc=com"
$domain = "dc=contoso,dc=com"

do
{

# Operator Variables.
$rootOUsource  = read-host "What is the SOURCE root OU name?"
$deptnamesource = read-host "What is the SOURCE department name?"

write-host "Retrieving user objects...." -ForegroundColor Magenta
start-sleep -s 1

#Retrieve object info.
Get-ADUser -Filter * -SearchBase "OU=$deptnamesource,OU=users,OU=$rootOUsource,$domain"
# Operator Variables.
$divnametarget = read-host "What is the TARGET division name?"
$deptnametarget = read-host "What is the TARGET department name?"

#Object/target variables
$users = Get-ADUser -Filter * -SearchBase "OU=$deptnamesource,OU=users,OU=$rootOUsource,$domain"
$targetOU = "OU=$deptnametarget,OU=$divnametarget,$path"
#Move object/s.
foreach ($user in $users) {
    Move-ADObject $user -TargetPath $targetOU

}
#THIS PART DOES NOT WORK CORRECTLY...
#Set AD Home Folder path and create/permission folder.
$newusers = Get-ADUser -Filter * -SearchBase $targetou
foreach ($newuser in $newusers) {
    set-aduser $newuser -homedirectory "\\server\share\users\%username%" -homedrive H:

}

#Repeat process option.
write-host "All objects have been moved." -ForegroundColor Magenta
$choice = read-host "Do you want to move more objects?"
$choice = $choice.toupper()


}
while ($choice -eq "Y")

Is there a "replacement" for %username% that can be used generically for a bunch of users at once?

Best Answer

I did just this very thing on Monday. Below is the script I used, modified for the purposes of posting here, obviously. What you're looking for, instead of %username% is $_.SamAccountName

Get-ADUser -SearchBase "OU=Users,OU=[site],DC=[company],DC=[tld]" | % { Set-ADUser $_ -HomeDrive "H:" -HomeDirectory ('\\[dfs root]\[remote site name]\Users\' + $_.SamAccountName) }

(And yes, this does prompt for a filter value, because we don't have only users in our Users OUs. If you want to eliminate that, you'd add -Filter * in there.)