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?


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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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