These commands help you create and change user accounts in Active Directory easily.
New-ADUser and Set-ADUser in PowerShell
Start learning this pattern below
Jump into concepts and practice - no test required
New-ADUser -Name <string> -GivenName <string> -Surname <string> -SamAccountName <string> [-OtherParameters] Set-ADUser -Identity <string> [-OtherParameters]
New-ADUser creates a new user account with required details like name and username.
Set-ADUser changes existing user details using the user's identity (like username).
New-ADUser -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -AccountPassword (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) -Enabled $true
Set-ADUser -Identity "jdoe" -OfficePhone "123-456-7890"
This script first creates a new user named Alice Smith with username 'asmith' and a password. Then it sets her job title to 'Sales Manager'. Finally, it shows her name, username, and title to confirm the changes.
Import-Module ActiveDirectory # Create a new user New-ADUser -Name "Alice Smith" -GivenName "Alice" -Surname "Smith" -SamAccountName "asmith" -AccountPassword (ConvertTo-SecureString "Welcome123" -AsPlainText -Force) -Enabled $true # Update the user's title Set-ADUser -Identity "asmith" -Title "Sales Manager" # Get user info to confirm Get-ADUser -Identity "asmith" -Properties Title | Select-Object Name, SamAccountName, Title | Format-List
You need to run these commands with permissions to manage Active Directory users.
Passwords must be converted to secure strings before use with New-ADUser.
Always check if the user exists before creating or updating to avoid errors.
New-ADUser is for creating new user accounts.
Set-ADUser is for changing details of existing users.
Both commands help automate user management in Active Directory.
Practice
New-ADUser cmdlet in PowerShell?Solution
Step 1: Understand the cmdlet purpose
New-ADUseris designed to create new user accounts in Active Directory.Step 2: Compare with other options
Deleting users, resetting passwords, or listing users are done by other cmdlets likeRemove-ADUser,Set-ADAccountPassword, orGet-ADUser.Final Answer:
To create a new user account in Active Directory -> Option DQuick Check:
New-ADUser creates users = A [OK]
- Confusing New-ADUser with Set-ADUser
- Thinking it deletes users
- Assuming it lists users
New-ADUser?Solution
Step 1: Identify correct parameters for New-ADUser
The parameter for username is-SamAccountName, and for display name is-DisplayName.Step 2: Check each option
New-ADUser -SamAccountName 'jdoe' -DisplayName 'John Doe' uses-SamAccountName 'jdoe'and-DisplayName 'John Doe', which is correct syntax.Final Answer:
New-ADUser -SamAccountName 'jdoe' -DisplayName 'John Doe' -> Option AQuick Check:
SamAccountName sets username = A [OK]
- Using -UserName instead of -SamAccountName
- Using -Name instead of -DisplayName
- Mixing parameter names incorrectly
New-ADUser -SamAccountName 'asmith' -Name 'Alice Smith' -AccountPassword (ConvertTo-SecureString 'P@ssw0rd' -AsPlainText -Force) -Enabled $true Set-ADUser -Identity 'asmith' -Title 'Manager'
What is the Title property of user 'asmith' after running these commands?
Solution
Step 1: Create user with New-ADUser
The user 'asmith' is created with name 'Alice Smith' and password set, enabled account.Step 2: Update user with Set-ADUser
TheSet-ADUsercommand sets theTitleproperty to 'Manager' for user 'asmith'.Final Answer:
Manager -> Option BQuick Check:
Set-ADUser updates Title = C [OK]
- Assuming Title is set by New-ADUser without parameter
- Confusing password with Title property
- Thinking Title remains empty
Set-ADUser -Identity 'bwhite' -Department 'Sales'
But you get an error: "Cannot find an object with identity: 'bwhite'". What is the most likely cause?
Solution
Step 1: Analyze the error message
The error says it cannot find an object with identity 'bwhite', meaning the user does not exist or the name is incorrect.Step 2: Check other options
Department can be set with Set-ADUser, New-ADUser is for creating users, and Identity accepts username or distinguished name, not necessarily email.Final Answer:
User 'bwhite' does not exist in Active Directory -> Option CQuick Check:
Identity error means user missing = D [OK]
- Assuming Department can't be set
- Using New-ADUser to update existing users
- Using wrong Identity format without verifying user
Solution
Step 1: Create user with New-ADUser
Use-SamAccountNameand-DisplayNameto create the user. Office location is not set here.Step 2: Update office location with Set-ADUser
UseSet-ADUser -Identity 'mjohnson' -Office 'HQ-5'to set the office property after creation.Step 3: Evaluate other options
Set-ADUser -SamAccountName 'mjohnson' -DisplayName 'Mary Johnson' New-ADUser -Identity 'mjohnson' -Office 'HQ-5' tries to update before creation, which fails. New-ADUser -UserName 'mjohnson' -Name 'Mary Johnson' -Office 'HQ-5' -Enabled $true uses wrong parameters. New-ADUser -SamAccountName 'mjohnson' -DisplayName 'Mary Johnson' -Office 'HQ-5' -Enabled $true fails because -Enabled $true requires -AccountPassword (e.g., (ConvertTo-SecureString 'P@ssw0rd' -AsPlainText -Force)), which is missing.Final Answer:
New-ADUser -SamAccountName 'mjohnson' -DisplayName 'Mary Johnson' Set-ADUser -Identity 'mjohnson' -Office 'HQ-5' -> Option AQuick Check:
Create then update properties = B [OK]
- Trying to set unsupported properties in New-ADUser
- Running Set-ADUser before user exists
- Using wrong parameter names
