0
0
PowerShellscripting~5 mins

New-ADUser and Set-ADUser in PowerShell

Choose your learning style9 modes available
Introduction

These commands help you create and change user accounts in Active Directory easily.

When you want to add a new employee to your company's user system.
When you need to update a user's details like their phone number or department.
When automating user account setup for many people at once.
When fixing mistakes in user information without using a manual tool.
Syntax
PowerShell
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).

Examples
This creates a new user named John Doe with a username 'jdoe' and sets a password.
PowerShell
New-ADUser -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -AccountPassword (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) -Enabled $true
This updates the phone number of the user with username 'jdoe'.
PowerShell
Set-ADUser -Identity "jdoe" -OfficePhone "123-456-7890"
Sample Program

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.

PowerShell
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
OutputSuccess
Important Notes

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.

Summary

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.