Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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.)

share|improve this answer
    
Thank you! I appreciate it. I'll give it a shot. –  j7wb Apr 9 '13 at 22:55
add comment

You were right! $_.SamAccountName was the way to go.

Just an FYI. I found this below and it is working for me. It looks quite similar to your example. Thanks!


#Set AD Home Folder path.
Get-ADUser -Filter * -SearchBase $targetou | Foreach-Object {
    $sam = $_.SamAccountName
    Set-ADuser -Identity $_ -HomeDrive "H:" -HomeDirectory "\\server\share\$sam"
}
share|improve this answer
    
You've posted this as an answer, but the first half reads more like a comment on this answer. On SE sites, unlike many forums, answers do not appear in chronological order. Please be sure to mark accepted one of the functional answers. –  jscott Apr 10 '13 at 0:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.