Adding a object(User) to a OU from a powershell

Qasim Afzal 101 Reputation points
2022-11-20T06:23:07.597+00:00

I have checked the command and the format for the script not sure what's wrong.
262117-user-and-computer.jpg262163-adding-a-user-on-powershell-script-error.jpg

262192-part2-script-powershell.jpg
I have added the screenshot for proof and the error message.

I switched the OU path to see if it would make any difference.. but yet it didn't.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,820 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sandeep G-MSFT 14,241 Reputation points Microsoft Employee
    2022-11-21T10:13:01.193+00:00

    @Qasim Afzal

    I tried the same command and it worked for me as expected in my lab. I used below command,

    262643-image.png

    You can also try to create the user first using powershell and then move the user to specific OU using below command,

    Move-ADObject -Identity "CN=expiry,CN=Users,DC=magic,DC=local" -TargetPath "OU=test,OU=Sync,DC=magic,DC=local"

    Even this command worked for me in my lab.

    262586-image.png

    Do let me know if you have any further questions.

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 44,621 Reputation points
    2022-11-20T15:54:44.703+00:00

    Don't try to do everything on one line. Place the Read-Host before the Add-NewADUser and store the secure string in a variable. Use that variable as the value for the -AccountPassword parameter.