DEV Community

Hybrid SysAdmin
Hybrid SysAdmin

Posted on

Create an AD user with PowerShell: what New-ADUser needs and where it breaks

Note: all names, commands and output in this article are examples from a demo lab. Nothing here is a real tenant or real data. Test every step in your own lab first.

A new hire starts Monday and the first job is the same every time: create the Active Directory account. New-ADUser does it in one line. The trouble is what the one line leaves out.

The shortest version

New-ADUser -Name 'ChewDavid' -SamAccountName 'chewdavid'
Enter fullscreen mode Exit fullscreen mode

SamAccountName is the only parameter you must give. Leave it out and the cmdlet will not create the user.

Run that and you get an account. It is not ready to use.

What that account is missing

  • It is disabled. Accounts are created without a password by default, and a user account is never enabled unless a valid password is set.
  • It has no password. You have to pass one.
  • It sits in the default Users container. If you do not give -Path, the new user goes to the Users container, not an OU.

Add the password and enable it

New-ADUser -Name 'ChewDavid' -SamAccountName 'chewdavid' `
  -AccountPassword (Read-Host -AsSecureString 'AccountPassword') `
  -Enabled $true
Enter fullscreen mode Exit fullscreen mode

Read-Host -AsSecureString asks for the password at the prompt, so it never sits in a script file or your shell history. -Enabled $true turns the account on. Microsoft's page says an enabled account requires a password, which is why the two go together.

Put it in the right place

Add -Path with the distinguished name of the OU:

New-ADUser -Name 'Mona Samir' -SamAccountName 'mona.samir' `
  -UserPrincipalName 'mona.samir@demo-lab.example' `
  -Path 'OU=Sales,OU=Users,DC=demo-lab,DC=local' `
  -AccountPassword (Read-Host -AsSecureString 'AccountPassword') `
  -ChangePasswordAtLogon $true -Enabled $true
Enter fullscreen mode Exit fullscreen mode

-ChangePasswordAtLogon $true makes the user pick their own password at first sign-in. One limit from the docs: you cannot combine it with a password that never expires.

Where it breaks

  1. The password is rejected. If the password you give fails the domain password policy, the account is still created. It stays disabled and has no password. Set one afterwards with Set-ADAccountPassword, then enable it.
  2. The user lands in the wrong place. No -Path means the Users container. If your sync or your group policy depends on an OU, check where the object really went.
  3. The SamAccountName is long. For old-system compatibility, keep it to 20 characters or fewer.
  4. You type the same line 40 times. For many users, read a CSV with Import-Csv and pipe the rows into New-ADUser. Test it on one row first.

The checklist

  • SamAccountName, always
  • Password from a secure prompt
  • Enabled only after a valid password
  • Path set to the right OU

I made a 30 second video of this with demo-lab screens and an AI-generated voiceover. Every screen is labeled as a demo and the output is simulated.

Questions, or a step that differs in your environment? Tell me in the comments.

Written by Hybrid SysAdmin. The commands and screens here are demo-lab examples, not a recording of a real environment.

Source: https://learn.microsoft.com/en-us/powershell/module/activedirectory/new-aduser?view=windowsserver2025-ps

Onboarding automation script: https://fadyverse10.gumroad.com/l/Automation

Top comments (0)