Admin Power Level: Intermediate Chapter 11: Manage People, Computers, and Names
AD users: create, read, change, and remove
User accounts live in Active Directory as objects. The AD user cmdlets create, read, change, and remove them, and they need the Active Directory module and domain permissions.
Lesson 1 of 6 in Chapter 11: Manage People, Computers, and Names. Previous step in this path: Enter-PSSession: an interactive remote session. 3 learning objectives, 7 examples, and a knowledge check.
Start: what you will learn
User accounts live in Active Directory as objects. The AD user cmdlets create, read, change, and remove them, and they need the Active Directory module and domain permissions.
Learning objectives
- Create a user with New-ADUser
- Read users with Get-ADUser and a filter
- Change and remove users safely
Concept in simple words
What it is: AD user management means creating, reading, changing, and removing user accounts in Active Directory, which is the directory service that holds users, computers, and groups for a domain.
Why it works
Why it works this way: A user account is stored as an object in the directory, with properties, which are facts such as Name, SamAccountName, and Enabled. SamAccountName is the short logon name, and Enabled tells you whether the account can log on. The AD cmdlets read and change that object, and they only work when the Active Directory module is loaded and you have domain permission to do the task.
Think of it like this
Think of it like this: Managing domain users is like running the front desk of a building. You create a badge for a new person, you look up who has a badge, you update the details when someone changes team, and you take the badge away when someone leaves, and you always check the name on the badge before you act.
How it works, step by step
How it works, step by step: First, make sure you are in a proper server environment with the Active Directory module and the right permissions, because these commands are not meant for a normal workstation, and they change a shared directory. Then create a user with New-ADUser, giving it a Name, a SamAccountName, and, in real use, a password and an organisational unit path, which is the location of the container where the user should live.
Next, read users back with Get-ADUser, using -Identity for one user by logon name, or -Filter for a pattern, and add -Properties when you need facts that are not shown by default, such as Department. Change a user with Set-ADUser, and remove one with Remove-ADUser, but before any removal, read the user back first and use -Confirm, because deletion is hard to undo.
Let us walk through an example: In Get-ADUser -Filter { Name -like "Iz*" } | Select-Object Name, SamAccountName, Enabled, the filter asks the domain for users whose names start with Iz, where -like means a wildcard match and the star means any ending. Look for the Name, the short logon name, and the Enabled value in the output, and notice that filtering on the domain side keeps the result small, instead of pulling every user in a large domain.
When to use it
When to use it, and when not to: Use these cmdlets when you manage domain accounts in a server lab or in a domain you are allowed to administer, and when you have checked the exact account first. Do not use them on a normal workstation, do not use Get-ADUser -Filter * on a large domain when a narrow filter would do, and do not remove a user you have not read back and confirmed by name. For a local account on one computer only, use Get-LocalUser instead, because it does not touch the domain.
Start from zero, with the directory itself: Active Directory is a shared database that domain controllers keep for a whole domain. A local account lives on one computer only, and it means nothing to any other computer. A domain account lives in that shared database, so any domain computer can check it, and one change in the directory is visible across the domain after replication. That shared nature is why these commands are powerful, and why they need care.
What happens behind the command: When you run Get-ADUser, PowerShell does not search your own computer. The ActiveDirectory module sends a directory query to a domain controller, the controller searches its copy of the directory, and PowerShell turns the matching objects into rows you can read. When you run New-ADUser or Set-ADUser, PowerShell sends a change request to a domain controller, and that controller writes the change and then copies it to other controllers. If the module is missing, PowerShell does not recognise the cmdlet at all. If you have no network path to a domain controller, the query cannot reach the directory. If you have no permission for that task, the controller refuses the change.
Three names for one person, and why they are different: Name is the display name of the object, such as Izaz Haider. SamAccountName is the short logon name, such as izaz, and it is the name you type at an older style logon prompt. UserPrincipalName is the longer logon name in the form of a name plus an at sign plus a domain suffix, and it is the name you often type at a modern logon prompt. Keep them consistent and simple, and check for an existing SamAccountName before you create a user, because two users cannot share the same short logon name in one domain.
Let us walk through a creation, part by part: In New-ADUser -Name "Izaz Haider" -SamAccountName "izaz" -Enabled $true, -Name sets the display name, -SamAccountName sets the short logon name, and -Enabled $true asks for the account to be usable at once. In real use, also add -Path with the distinguished name of the OU where the user should live, and -AccountPassword with a secure password, because a user with no password and no clear home OU is hard to manage later. After you create the user, read it back. Creation alone does not prove the account is enabled, in the right OU, or ready to log on.
Let us walk through a change, part by part: In Set-ADUser -Identity izaz -Department "IT" -Title "Student", -Identity picks exactly one user by logon name, -Department writes the department fact, and -Title writes the job title fact. Only the facts you name change. Everything else stays as it was. That is why Set-ADUser is safer than recreating a user when only a detail has changed, because recreating would lose group memberships and history tied to the original object.
Let us walk through a safe password step: First run $password = Read-Host -AsSecureString "Enter password". Read-Host asks you to type, -AsSecureString keeps the typed text as a secure string instead of plain readable text, and the result is stored in the variable $password. Then pass -AccountPassword $password to New-ADUser. Do not type a real password into a shared script file, and do not paste a real password into a chat or a document, because a password written in plain text can be copied by anyone who later opens that file.
How to search a small part of the directory: Add -SearchBase to limit the search to one OU, for example Get-ADUser -Filter * -SearchBase "OU=Sales,DC=example,DC=local". -SearchBase says start here and do not look outside this container. That keeps the result small, it keeps the query fast, and it stops you from accidentally acting on a user with a similar name in a different OU. Use a broad filter only when you truly need every user, and even then, ask for only the properties you need.
What Get-ADUser shows by default, and what it hides: By default you get a small set of facts, including DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, and UserPrincipalName. Facts such as Department, Title, PasswordLastSet, and PasswordExpired are extended facts, and they stay hidden until you ask for them with -Properties. A blank Department column does not always mean the user has no department. It can also mean you never asked for Department, so check your -Properties list before you conclude that a fact is missing.
Disable before you delete, for leavers: When someone leaves temporarily, or when you are not yet sure, disable the account with Set-ADUser -Identity izaz -Enabled $false instead of deleting it. A disabled account cannot log on, but the object, its group memberships, and its history stay in place, so you can enable it again if the person returns. Deletion removes the object, and that loss is hard to undo, even when the person returns the next week. Delete only when you are sure, after you have read the account back and confirmed the name.
Use -WhatIf as a rehearsal: Before a removal, run the same command with -WhatIf added, for example Remove-ADUser -Identity izaz -WhatIf. PowerShell then describes what it would do, without doing it. Read that description slowly. If the name in the description is not the name you meant, stop. -WhatIf does not fix a wrong identity, it only shows you the mistake before it becomes permanent, and that early view is worth the extra few seconds every time.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
New-ADUser -Name <Name> -SamAccountName <Logon> -AccountPassword <SecureString>Example 1
Creates a domain user with a logon name, enabled at once. In real use, also set a password and an organisational unit path.
New-ADUser -Name "Izaz Haider" -SamAccountName "izaz" -Enabled $trueIllustrative output: User izaz created.
Example 2
Filters on the domain side, then shows three useful properties per user.
Get-ADUser -Filter { Name -like "Iz*" } | Select-Object Name, SamAccountName, EnabledIllustrative output: Name SamAccountName Enabled ---- -------------- ------- Izaz Haider izaz True
Example 3
-Identity izaz reads one user by logon name, which is safer than filtering a long list when you already know who you mean. -Properties Department, Title asks for two extra facts that are not shown by default. Select-Object Name, Department, Title then shows only those three facts, so the output is easy to read and easy to check.
Get-ADUser -Identity izaz -Properties Department, Title | Select-Object Name, Department, TitleIllustrative output: Name Department Title ---- ---------- ----- Izaz Haider IT Student
Example 4
Changes two facts on one user, and leaves everything else unchanged. Read the user back after the change to confirm both new values.
Set-ADUser -Identity izaz -Department "IT" -Title "Student"Illustrative output: User izaz updated.
Example 5
Limits the search to the Sales OU only, so you get a small, fast result and you do not mix up users from other OUs.
Get-ADUser -Filter * -SearchBase "OU=Sales,DC=example,DC=local" | Select-Object Name, SamAccountName, EnabledIllustrative output: Name SamAccountName Enabled ---- -------------- ------- Izaz Haider izaz True
Example 6
Warning: this creates an account, so use it only in your own lab domain, with permission. The first line asks for a password as a secure string. The second line creates the user with that password, enabled at once.
$password = Read-Host -AsSecureString "Enter password"
New-ADUser -Name "Izaz Haider" -SamAccountName "izaz" -AccountPassword $password -Enabled $trueIllustrative output: User izaz created.
Example 7
Disables the account instead of deleting it. The user cannot log on, but the object and its memberships stay in place, so this is the safer choice when you are not sure the account should be gone for good.
Set-ADUser -Identity izaz -Enabled $falseIllustrative output: User izaz disabled.
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Diagram
Choose a stage above to see what it does.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Using Get-ADUser -Filter * on a large domain and waiting for thousands of objects. Filter early.
- Removing a user without first reading it back with Get-ADUser to confirm it is the right one.
- Creating a user and then assuming it is enabled and ready to log on, without reading it back, when new users are often created disabled, so always check the Enabled value after you create one.
- Asking for Department or Title in Select-Object without adding -Properties Department, Title to Get-ADUser, then seeing a blank column and wrongly concluding that the user has no department.
- Creating a user with a SamAccountName that already exists, then being surprised by an error, when a quick Get-ADUser -Identity check first would have shown the clash.
- Deleting an account for a temporary leaver instead of disabling it, then losing the object and its memberships when the person returns.
- Using -Confirm:$false on a removal in an untested script, which removes the confirmation question that would have caught a wrong identity.
Knowledge check
Knowledge check
Which cmdlet reads domain users?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: Create carefully, read back before you change or remove anything, filter early so you only handle the accounts you mean, and always work in a server environment with the right module and the right permissions.
Next step: AD computers: join, read, and disconnect