1

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?

1 Answer 1

0

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} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.