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

1 Answer 1

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

 
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.