Feeding Variables in new-aduser -path option in powershell

2.7k views Asked by At

Here is the Command I am trying to work with:

New-ADUser -name "$firstName $lastName" -SamAccountName "$firstName.$lastName" -GivenName "$firstName" -Surname "$lastName" -DisplayName "$firstName $lastName" -Path "OU=Employees,OU=$Dpart,DC=OPR,DC=Local" -Enabled $true -AccountPassword $PWD -ChangePasswordAtLogon $true -EmailAddress "[email protected]"

The Error Message I get:

ObjectNotFound: (CN=FIRST LAST...DC=OPR,DC=Local:String)

Once I remove the $Dpart from the -Path the command fires off correctly, but does not place the person into the correct OU. The command is filtered before hand and matches the name inside AD of the sub OU.

How can I use a variable inside the path command? I know it's simple, but I am just starting off with powershell.

1

There are 1 answers

0
David On
New-ADUser -name "$firstName $lastName" -SamAccountName "$firstName.$lastName" -GivenName "$firstName" -Surname "$lastName" -DisplayName "$firstName $lastName" -Path "OU=$Dpart,OU=Employees,DC=OPR,DC=Local" -Enabled $true -AccountPassword $PWD -ChangePasswordAtLogon $true -EmailAddress "[email protected]"

It was so obvious that a snake would have bit me...

Lowest level OU first, then each level up after that. So, OU=$Dpart, OU=Employees