Hello,
I have New-ADUser in a ForEach-Object loop. It works great until I try to set the -path using variables. The variable values come from a CSV I am importing…
$newStaff | ForEach-Object {
New-ADUser -Name ($_.FirstName + ' ' + $_.LastName) `
-Surname $_.LastName `
-DisplayName $_.DisplayName `
-GivenName $_.FirstName `
-EmailAddress $_.EmailAddress `
-SamAccountName $_.UserLogonName `
-UserPrincipalName ($_.UserLogonName + '@domain.com') `
-Title ($_.PositionTitle) `
-HomeDrive "H" `
-HomeDirectory ($_.HomeServer + '\home\' + $_.UserLogonName) `
-Path "OU=TS,OU=STAFF,OU=User Accounts,DC=mydomain,DC=mydomain" `
-AccountPassword (ConvertTo-SecureString "temppassword" -AsPlainText -force) `
-ChangePasswordAtLogon $true `
-Enabled $true `
}
The above works. The below does not. Only difference is in the -path statement…
-Path "OU=$_.LocationOU,OU=$_.ElemOrSecOU,OU=$_.CertOU,OU=User Accounts,DC=rqs,DC=C2"
$_.LocationOU is a column in $newStaff. The new users will be going to different nested OU’s.
I have tried loading $_.LocationOU into $location, and using -path "OU=$location, etc. but I get the error…
New-ADUser : The object name has bad syntax
The error is on the -Name property.
Any advice appreciated. Thank you!