How to Create an AD User with PowerShell Quickly
Use the
New-ADUser cmdlet in PowerShell to create a new Active Directory user. You specify user details like -Name, -SamAccountName, and -AccountPassword to create the user account.Syntax
The New-ADUser cmdlet creates a new user in Active Directory. Key parameters include:
-Name: The full name of the user.-SamAccountName: The user's logon name.-AccountPassword: The user's password as a secure string.-Enabled: Whether the account is active (true or false).-Path: The organizational unit (OU) where the user will be created.
powershell
New-ADUser -Name <string> -SamAccountName <string> -AccountPassword <securestring> -Enabled <bool> [-Path <string>]
Example
This example creates a new AD user named "John Doe" with a username "jdoe" in the "Users" container. It sets a password and enables the account.
powershell
$password = ConvertTo-SecureString "P@ssw0rd123" -AsPlainText -Force New-ADUser -Name "John Doe" -SamAccountName "jdoe" -AccountPassword $password -Enabled $true -Path "CN=Users,DC=example,DC=com"
Common Pitfalls
Common mistakes when creating AD users include:
- Not converting the password to a secure string, which causes errors.
- Forgetting to enable the account, leaving it disabled by default.
- Using an incorrect
-Path, which places the user in the wrong OU or causes failure. - Missing required parameters like
-SamAccountNameor-Name.
powershell
## Wrong way (password as plain text, account disabled by default) New-ADUser -Name "Jane Smith" -SamAccountName "jsmith" -AccountPassword "P@ssw0rd123" ## Right way $password = ConvertTo-SecureString "P@ssw0rd123" -AsPlainText -Force New-ADUser -Name "Jane Smith" -SamAccountName "jsmith" -AccountPassword $password -Enabled $true
Quick Reference
| Parameter | Description |
|---|---|
| -Name | Full name of the user |
| -SamAccountName | User logon name |
| -AccountPassword | User password as secure string |
| -Enabled | Enable or disable the account (true/false) |
| -Path | Active Directory container or OU for the user |
Key Takeaways
Always convert passwords to secure strings using ConvertTo-SecureString before using -AccountPassword.
Use -Enabled $true to activate the user account immediately after creation.
Specify the correct -Path to place the user in the desired Active Directory container.
Provide both -Name and -SamAccountName parameters as they are required.
Test your script in a safe environment before running in production.