Active Directory PowerShell
Active Directory PowerShell cmdlets for domain and user management.
DC Shutdown Procedure
Step 1: Check FSMO rolesTransfer roles if needed before shutdown
netdom query fsmoDisplay FSMO role holders
Step 2: Force replicationEnsure changes are replicated
repadmin /syncall /AdePForce sync with all partners
Step 3: Notify usersWarn of maintenance window
Step 4: Graceful shutdownShutdown or restart DC
Stop-Computer -ForceShutdown computer
Restart-Computer -ForceRestart computer
shutdown /s /t 60 /c "DC Maintenance"Shutdown with 60s warning
Module Setup
Import-Module ActiveDirectoryImport Active Directory PowerShell module
Get-Module -ListAvailable ActiveDirectoryCheck if AD module is installed
Install-WindowsFeature RSAT-AD-PowerShellInstall AD PowerShell module (Server)
Get-Command -Module ActiveDirectoryList all AD cmdlets
Get-Help Get-ADUser -FullGet detailed help for AD cmdlet
User Management
Get-ADUser -Filter *Get all AD users
Get-ADUser -Identity usernameGet specific user by username
Get-ADUser -Filter {Enabled -eq $true}Get all enabled users
Get-ADUser -Filter * -Properties *Get all users with all properties
New-ADUser -Name "John Doe" -SamAccountName jdoe -UserPrincipalName jdoe@domain.comCreate new AD user
Set-ADUser -Identity username -Description "New description"Modify user properties
Set-ADAccountPassword -Identity username -Reset -NewPassword (ConvertTo-SecureString "Password123" -AsPlainText -Force)Reset user password
Enable-ADAccount -Identity usernameEnable user account
Disable-ADAccount -Identity usernameDisable user account
Unlock-ADAccount -Identity usernameUnlock locked user account
Remove-ADUser -Identity username -Confirm:$falseDelete user account
Search-ADAccount -LockedOutFind all locked out accounts
Search-ADAccount -AccountDisabledFind all disabled accounts
Search-ADAccount -AccountExpiredFind all expired accounts
Search-ADAccount -PasswordExpiredFind accounts with expired passwords
Group Management
Get-ADGroup -Filter *Get all AD groups
Get-ADGroup -Identity "Group Name"Get specific group
Get-ADGroupMember -Identity "Group Name"Get group members
Get-ADGroupMember -Identity "Group Name" -RecursiveGet group members recursively
New-ADGroup -Name "New Group" -GroupScope Global -GroupCategory SecurityCreate new security group
Add-ADGroupMember -Identity "Group Name" -Members usernameAdd user to group
Remove-ADGroupMember -Identity "Group Name" -Members username -Confirm:$falseRemove user from group
Get-ADPrincipalGroupMembership -Identity usernameGet user group memberships
Remove-ADGroup -Identity "Group Name" -Confirm:$falseDelete group
Computer Management
Get-ADComputer -Filter *Get all AD computers
Get-ADComputer -Identity computernameGet specific computer
Get-ADComputer -Filter {Enabled -eq $true}Get all enabled computers
Get-ADComputer -Filter * -Properties *Get all computers with all properties
New-ADComputer -Name "COMPUTER01" -Path "OU=Computers,DC=domain,DC=com"Create new computer object
Set-ADComputer -Identity computername -Description "New description"Modify computer properties
Remove-ADComputer -Identity computername -Confirm:$falseDelete computer object
Test-ComputerSecureChannel -Server computernameTest computer trust relationship
Reset-ComputerMachinePassword -Server DC01Reset computer account password
Organizational Unit (OU) Management
Get-ADOrganizationalUnit -Filter *Get all OUs
Get-ADOrganizationalUnit -Identity "OU=Users,DC=domain,DC=com"Get specific OU
New-ADOrganizationalUnit -Name "New OU" -Path "DC=domain,DC=com"Create new OU
Set-ADOrganizationalUnit -Identity "OU=Users,DC=domain,DC=com" -Description "New description"Modify OU properties
Remove-ADOrganizationalUnit -Identity "OU=Test,DC=domain,DC=com" -Confirm:$falseDelete OU
Set-ADOrganizationalUnit -Identity "OU=Users,DC=domain,DC=com" -ProtectedFromAccidentalDeletion $trueProtect OU from deletion
Domain & Forest Management
Get-ADDomainGet current domain information
Get-ADForestGet forest information
Get-ADDomainControllerGet all domain controllers
Get-ADDomainController -DiscoverDiscover nearest domain controller
Get-ADDomainController -Filter *Get all DCs in domain
Test-ComputerSecureChannelTest domain trust
Get-ADReplicationSite -Filter *Get all AD sites
Get-ADReplicationSubnet -Filter *Get all AD subnets
Get-ADTrust -Filter *Get all domain trusts
Group Policy Management
Get-GPO -AllGet all Group Policy Objects
Get-GPO -Name "GPO Name"Get specific GPO
New-GPO -Name "New GPO"Create new GPO
Set-GPLink -Name "GPO Name" -Target "OU=Users,DC=domain,DC=com"Link GPO to OU
Get-GPOReport -Name "GPO Name" -ReportType Html -Path C:\report.htmlGenerate GPO report
Invoke-GPUpdate -ForceForce group policy update
gpresult /rDisplay applied group policies
gpresult /h C:\gpreport.htmlGenerate HTML GPResult report
Password & Security Policies
Get-ADDefaultDomainPasswordPolicyGet default password policy
Set-ADDefaultDomainPasswordPolicy -Identity domain.com -MaxPasswordAge 90Set password max age
Get-ADFineGrainedPasswordPolicy -Filter *Get all fine-grained password policies
New-ADFineGrainedPasswordPolicy -Name "Policy Name" -Precedence 10 -MinPasswordLength 12Create fine-grained password policy
Add-ADFineGrainedPasswordPolicySubject -Identity "Policy Name" -Subjects "Group Name"Apply password policy to group
Get-ADUserResultantPasswordPolicy -Identity usernameGet effective password policy for user
Reporting & Auditing
Get-ADUser -Filter * -Properties LastLogonDate | Select Name,LastLogonDateGet user last logon dates
Get-ADUser -Filter {LastLogonDate -lt (Get-Date).AddDays(-90)}Find inactive users (90 days)
Get-ADComputer -Filter {LastLogonDate -lt (Get-Date).AddDays(-90)}Find inactive computers (90 days)
Get-ADUser -Filter * -Properties PasswordLastSet | Select Name,PasswordLastSetGet password last set dates
Get-ADUser -Filter * -Properties Created | Select Name,CreatedGet user creation dates
Get-ADGroup -Filter * | Measure-ObjectCount total groups
Get-ADUser -Filter * | Measure-ObjectCount total users
Get-ADComputer -Filter * | Measure-ObjectCount total computers
Bulk Operations
Import-Csv users.csv | ForEach-Object {New-ADUser -Name $_.Name -SamAccountName $_.Username}Bulk create users from CSV
Get-ADUser -Filter * | Set-ADUser -City "New York"Bulk update user attribute
Get-ADUser -Filter * -SearchBase "OU=ToDisable,DC=domain,DC=com" | Disable-ADAccountBulk disable users in OU
Get-ADUser -Filter {Enabled -eq $false} | Remove-ADUser -Confirm:$falseBulk delete disabled users
Get-ADComputer -Filter {OperatingSystem -like "*Windows 7*"} | Remove-ADComputer -Confirm:$falseBulk remove computers by OS
LDAP Queries
Get-ADUser -LDAPFilter "(mail=*@domain.com)"Find users by email domain
Get-ADUser -LDAPFilter "(department=IT)"Find users by department
Get-ADObject -LDAPFilter "(objectClass=*)"Get all AD objects
Get-ADUser -Filter * -SearchBase "OU=Users,DC=domain,DC=com"Search within specific OU
Get-ADUser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"Find all enabled users (LDAP)
Replication & Troubleshooting
repadmin /showreplDisplay replication status
repadmin /replsummaryDisplay replication summary
repadmin /syncall /AdePForce replication across all DCs
dcdiag /vRun domain controller diagnostics
dcdiag /test:dnsTest DNS on domain controller
nltest /dsgetdc:domain.comLocate domain controller
Get-ADReplicationFailure -Target DC01Get replication failures for DC
Get-ADReplicationPartnerMetadata -Target DC01Get replication partner info