How to Set Up Active Directory and DHCP with PowerShell

Adam Bertram

Adam Bertram

Read more posts by this author.

I’m currently building a test lab for an upcoming Powershell course. I’m trying to do EVERYTHING for this test lab in PowerShell. Normally, I’ll just get lazy and go back to my GUI ways sometimes when I don’t know the cmdlet off hand. Anyway, I’ll try to share some of the scripts I’m using to get everything up and going. Here’s my first contribution.

To preface this set of scripts, I’ve got a blank Windows Server 2012 R2 server using DHCP for its IP. I want to make this server an Active Directory domain controller and a DHCP server to get my test lab started.

Alot of this was inspired by this Hey Scripting Guy blog post.


## Set a static IP
$ipaddress = ''
$ipprefix = '24'
$ipgw = ''
$ipdns = ''
$ipif = (Get-NetAdapter).ifIndex
New-NetIPAddress -IPAddress $ipaddress -PrefixLength $ipprefix� -InterfaceIndex $ipif

## Set the NIC to use itself as the DNS server since this is going to be a domain controller
Set-DnsClientServerAddress -InterfaceIndex $ipif -ServerAddresses $ipdns

## Rename the server and restart it to commit the change
$newname = 'TEST-DC'
Rename-Computer -NewName $newname -force
Restart-Computer -Force


## Install the bits for Active Directory Domain Services
Install-WindowsFeature 'AD-Domain-Services'� -IncludeAllSubFeature -IncludeManagementTools

# Create New Forest, add Domain Controller
$domainname = 'test.local'
$netbiosName = 'TEST'
Import-Module ADDSDeployment

$ad_params = @{
    SafeModeAdministratorPassword=(ConvertTo-SecureString '[email protected]$$w0rd12' -AsPlainText -Force);

Install-ADDSForest @ad_params


## Install the bits on the server for DHCP
Install-WindowsFeature 'DHCP' -IncludeAllSubFeature -IncludeManagementTools

## Authorize the DHCP server in Active Directory
Add-DhcpServerInDC -DnsName test-dc.test.local -IPAddress

## Create an IPv4 DHCP scope
Add-DhcpServerv4Scope -Name 'Test Scope' -StartRange '' -EndRange '' -SubnetMask ''

## Set the DNS server to use for all clients to use
on the DHCP server
Set-DhcpServerv4OptionValue -DnsDomain test.local -DnsServer

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!