Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created the following script to take data from a csv and create new users in AD:

Import-Csv c:\newusers.csv | ForEach-Object -Process {New-QADUser -name $.'un' -ParentContainer 'CN=users,DC=domain,DC=local' -SamAccountName $.'un' -FirstName $.'fn' -LastName $.'ln' -description '6' -userprincipalname $.'un'  -UserPassword 'P@ssword' | Enable-QADUser | Set-QADUser -HomeDirectory '\srv1\home\$.un' -HomeDrive 'H:' -UserMustChangePassword $true | new-item c:\home\$_.un -type directory}

Everything seems to be working except for the new-item creation of the home folders. The -homedirectory and -homedrive commands work and they are named and mapped properly in the users profile but when I try to create the actual folders on the drive it creates a folder called $_.un and then throws errors for object already exists.

I have tried 'c:\home\$.un', c:\home\$.'un', c:\home\'$_.un' all with similar results. Can anyone shed some light on what is wrong with my syntax?

share|improve this question

You can overcome this issue by adding $un variable like this:

Import-Csv c:\newusers.csv | ForEach-Object -Process {$un=$_.un; New-QADUser -name $.'un' -ParentContainer 'CN=users,DC=domain,DC=local' -SamAccountName $.'un' -FirstName $.'fn' -LastName $.'ln' -description '6' -userprincipalname $.'un' -UserPassword 'P@ssword' | Enable-QADUser | Set-QADUser -HomeDirectory '\srv1\home\$.un' -HomeDrive 'H:' -UserMustChangePassword $true | new-item c:\home\$un -type directory}

share|improve this answer

Your Answer


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.