Windows DHCP Server Powershell Scripts
From OISecWiki
(Redirected from Change DHCP gateways on Windows DHCP Server with Powershell)
Disable all Scopes
# Deactivate ALL IPv4 DHCP scopes
# Run as Administrator
$DhcpServer = $env:COMPUTERNAME # Change this if targeting a remote server
try {
Import-Module DhcpServer -ErrorAction Stop
Write-Host "Retrieving all scopes from $DhcpServer..." -ForegroundColor Cyan
$scopes = Get-DhcpServerv4Scope -ComputerName $DhcpServer
if (-not $scopes) {
Write-Host "No scopes found." -ForegroundColor Yellow
return
}
Write-Host "`nThe following scopes will be DEACTIVATED:" -ForegroundColor Yellow
$scopes | Format-Table ScopeId, Name, State, StartRange, EndRange -AutoSize
$confirm = Read-Host "`nAre you sure you want to deactivate ALL these scopes? (yes/no)"
if ($confirm -ne "yes") {
Write-Host "Aborted by user." -ForegroundColor Red
return
}
Write-Host "`nDeactivating scopes..." -ForegroundColor Cyan
foreach ($scope in $scopes) {
try {
Set-DhcpServerv4Scope -ComputerName $DhcpServer -ScopeId $scope.ScopeId -State InActive -ErrorAction Stop
Write-Host " ✓ Deactivated: $($scope.ScopeId) ($($scope.Name))" -ForegroundColor Green
}
catch {
Write-Host " ✗ Failed: $($scope.ScopeId) - $($_.Exception.Message)" -ForegroundColor Red
}
}
Write-Host "`nDone. Current status:" -ForegroundColor Cyan
Get-DhcpServerv4Scope -ComputerName $DhcpServer |
Select-Object ScopeId, Name, State |
Format-Table -AutoSize
}
catch {
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
Export all active leases
# Export all current IPv4 DHCP leases (no ScopeId prompt)
# Run as Administrator
$DhcpServer = $env:COMPUTERNAME # Change this if running against a remote server
$ExportPath = "C:\Temp\DHCP_IPv4_Leases_$(Get-Date -Format 'yyyy-MM-dd_HHmm').csv"
try {
Import-Module DhcpServer -ErrorAction Stop
Write-Host "Getting all scopes from $DhcpServer..." -ForegroundColor Cyan
$allLeases = @()
Get-DhcpServerv4Scope -ComputerName $DhcpServer | ForEach-Object {
Write-Host " → Retrieving leases for scope $($_.ScopeId) ($($_.Name))" -ForegroundColor Gray
$leases = Get-DhcpServerv4Lease -ComputerName $DhcpServer -ScopeId $_.ScopeId -ErrorAction SilentlyContinue
if ($leases) {
$allLeases += $leases
}
}
if ($allLeases.Count -gt 0) {
$allLeases |
Select-Object IPAddress,
ScopeId,
ClientId,
HostName,
AddressState,
LeaseExpiryTime,
ClientType,
Description,
DnsRegistration,
DnsName |
Sort-Object ScopeId, IPAddress |
Export-Csv -Path $ExportPath -NoTypeInformation -Encoding UTF8
Write-Host "`nSuccessfully exported $($allLeases.Count) leases to:" -ForegroundColor Green
Write-Host $ExportPath -ForegroundColor Yellow
}
else {
Write-Host "No leases found." -ForegroundColor Yellow
}
}
catch {
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
Change Gateways
How to change the gateways on all DHCP scopes within 10.160.0.0/16 from .1 to .254
# =============================================
# Change Default Router (.1 -> .254) in DHCP Scopes
# Target: scopes inside 10.160.0.0/16
# =============================================
# Get all IPv4 scopes
$scopes = Get-DhcpServerv4Scope
foreach ($scope in $scopes) {
$network = $scope.ScopeId.IPAddressToString
# Check if the scope is inside 10.160.0.0/16
if ($network -like "10.160.*") {
# Get current Router option (003) for this scope
$routerOption = Get-DhcpServerv4OptionValue `
-ScopeId $scope.ScopeId `
-OptionId 3 -ErrorAction SilentlyContinue
if ($routerOption -and $routerOption.Value) {
$oldRouter = $routerOption.Value[0] # usually only one value
# Replace last octet .1 with .254
if ($oldRouter -like "*.*.*.1") {
$newRouter = $oldRouter -replace "\.1$", ".254"
Write-Host "Scope $network : Changing router from $oldRouter to $newRouter" -ForegroundColor Green
# Apply the change
Set-DhcpServerv4OptionValue `
-ScopeId $scope.ScopeId `
-OptionId 3 `
-Value $newRouter `
-Force
}
else {
Write-Host "Scope $network : Router is $oldRouter (not ending in .1) - skipped" -ForegroundColor Yellow
}
}
else {
Write-Host "Scope $network : No Router option configured" -ForegroundColor Yellow
}
}
}
Write-Host "Finished processing scopes in 10.160.0.0/16" -ForegroundColor Cyan
