How to Set Up Active Directory Using PowerShell
Overview
Section titled “Overview”This article explains how to use PowerShell on Windows Server to build an Active Directory (AD) environment and add a second Domain Controller (DC).
Variable Conventions
Section titled “Variable Conventions”The following variables represent environment-dependent values. Replace them with those appropriate for your setup.
| Variable | Example | Description |
|---|---|---|
<<DOMAIN_NAME>> | example.local | Active Directory domain name |
<<NETBIOS_NAME>> | EXAMPLE | NetBIOS name (recommended in uppercase) |
<<ADMIN_PASSWORD>> | P@ssw0rd! | DSRM (Directory Services Restore Mode) password |
<<DOMAIN_ADMIN>> | Administrator | Domain administrator account |
<<DOMAIN_ADMIN_PASSWORD>> | P@ssw0rd! | Domain administrator password (for automation) |
<<INTERFACE_ALIAS>> | Ethernet | Network adapter name (e.g., Ethernet, Ethernet0) |
<<PRIMARY_DC_IP>> | 192.168.1.10 | IP address of the primary domain controller |
Step 1: Install the AD DS Role on the First Server
Section titled “Step 1: Install the AD DS Role on the First Server”Install the Active Directory Domain Services (AD DS) role on the first server.
Install-WindowsFeature AD-Domain-Services -IncludeManagementToolsStep 2: Create a New Forest and Promote to Domain Controller
Section titled “Step 2: Create a New Forest and Promote to Domain Controller”Use PowerShell to create a new forest and promote the first server as the Primary Domain Controller.
Forest/Domain Functional Levels
Section titled “Forest/Domain Functional Levels”| Functional Level | Enum Name |
|---|---|
| Windows Server 2008 | Window2008 |
| Windows Server 2008 R2 | Window2008R2 |
| Windows Server 2012 | Window2012 |
| Windows Server 2012 R2 | Window2012R2 |
| Windows Server 2016 | WinThreshold |
| Windows Server 2025 | Win2025 |
💡 In this example, both forest and domain functional levels are set to Windows Server 2016 (WinThreshold).
Install-ADDSForest ` -DomainName "<<DOMAIN_NAME>>" ` -DomainNetbiosName "<<NETBIOS_NAME>>" ` -SafeModeAdministratorPassword (ConvertTo-SecureString "<<ADMIN_PASSWORD>>" -AsPlainText -Force) ` -InstallDNS:$true ` -ForestMode WinThreshold ` -DomainMode WinThreshold ` -Force⚠️ If prompted to reboot, run the following command:
Restart-ComputerStep 3: Verify the First Domain Controller
Section titled “Step 3: Verify the First Domain Controller”After promotion, confirm that the domain structure was successfully created.
Get-ADDomainGet-ADDomainControllerGet-Service DNSStep 4: Install the AD DS Role on the Second Server
Section titled “Step 4: Install the AD DS Role on the Second Server”Install the AD DS feature on the second server.
Install-WindowsFeature AD-Domain-Services -IncludeManagementToolsStep 5: Verify and Configure DNS Settings
Section titled “Step 5: Verify and Configure DNS Settings”Before promoting the second server, ensure DNS settings are correct.
The secondary DC must reference the primary DC’s DNS server for domain join and replication.
# Check current DNS settingsGet-DnsClientServerAddress
# Set DNS to primary DCSet-DnsClientServerAddress -InterfaceAlias "<<INTERFACE_ALIAS>>" -ServerAddresses "<<PRIMARY_DC_IP>>"⚠️ If the second DC points to itself as the DNS server, promotion will fail.
Always configure the primary DC as the first DNS server before promotion.
Step 6: Promote the Second Server as an Additional Domain Controller
Section titled “Step 6: Promote the Second Server as an Additional Domain Controller”# Credentials$User = "<<NETBIOS_NAME>>\<<DOMAIN_ADMIN>>"$Pass = ConvertTo-SecureString "<<DOMAIN_ADMIN_PASSWORD>>" -AsPlainText -Force$Cred = New-Object System.Management.Automation.PSCredential($User, $Pass)
Install-ADDSDomainController ` -DomainName "<<DOMAIN_NAME>>" ` -Credential $Cred ` -SafeModeAdministratorPassword (ConvertTo-SecureString "<<ADMIN_PASSWORD>>" -AsPlainText -Force) ` -InstallDNS:$true ` -Force⚠️ Reboot if prompted:
Restart-ComputerStep 7: Verify Domain Controller Configuration
Section titled “Step 7: Verify Domain Controller Configuration”Ensure both domain controllers are listed and properly replicating.
Get-ADDomainController -Filter *To check replication status in detail:
repadmin /replsummaryrepadmin /showreplStep 8: Check DNS and Time Synchronization (Optional)
Section titled “Step 8: Check DNS and Time Synchronization (Optional)”Verify DNS service and time synchronization on both DCs.
Get-Service DNSw32tm /query /statusConclusion
Section titled “Conclusion”By following these steps, you can use PowerShell to build a complete Active Directory environment and quickly deploy redundant domain controllers. Automating these steps ensures consistent, repeatable deployments across multiple sites or recovery environments.