Active Directory: How to List All Computers in OU using PowerShell

$OUpath = 'ou=Managers,dc=enterprise,dc=com'
$ExportPath = 'c:\data\computers_in_ou.csv'
Get-ADComputer -Filter * -SearchBase $OUpath | Select-object
DistinguishedName,DNSHostName,Name | Export-Csv -NoType $ExportPath

To export all computers in’s servers OU to machines.txt :

DSQUERY COMPUTER "OU=servers,DC=mydomain,DC=com" -o rdn -limit 1000 > c:\machines.txt

Verify if an update is installed

PS> $A = Get-Content -Path ./Servers.txt

PS> $A | ForEach-Object { if (!(Get-HotFix -Id KB957095 -ComputerName $_)) { Add-Content $_ -Path ./Missing-KB957095.txt }}


The $A variable contains computer names that were obtained by Get-Content from a text file. The objects in $A are sent down the pipeline to ForEach-Object

DNS scavenging_status

$zones = Get-Content “C:\Users\k054995\Desktop\test\input.txt”

foreach($zone in $zones)


$Output = Get-DnsServerZoneAging -Name $zone -ComputerName pgc2eu-dceu101 | Select-Object ZoneName,AgingEnabled,RefreshInterval,NoRefreshInterval


$Output | export-csv -Path “C:\Users\k054995\Desktop\test\eu1_reverse-lookupzone_scavenging_status.csv” -NoTypeInformation -Append


AD replication Test

#identify AD sites which differ in replication schedule from a reference site, here GC1

$RefscheduleRaw= ((Get-ADReplicationSite -Identity GC1).ReplicationSchedule.RawSchedule)
$Refschedule= ((Get-ADReplicationSite -Identity GC1).ReplicationSchedule)

#Set-ADReplicationSite -Identity test -ReplicationSchedule $Refschedule

$sites= (Get-ADForest).sites
$SiteList= @(”)

foreach($site in $sites){
$schedule= ”
$schedule= ((Get-ADReplicationSite -Identity $site).ReplicationSchedule.RawSchedule)
if(!$schedule -or ($schedule -ne $RefscheduleRaw)){
# $site


$siteList |Sort-Object

Powershell Get-PSADForestInfo

$ADForestInfo = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Schema

DHCP pool monitor

#DHCP Percent of Addresses Remaining Threshold

$DHCPLowPercentThreshold = 10

#From Email Address

$FromAddress = “”

#To Email Address

$ToAddress = “”

#SMTP Server Address

$SmtpServerAddress = “”


$5MinutesAgo = [DateTime]::Now.AddMinutes(-5)

$DHCPLowEvent = @(Get-EventLog -LogName “System” -After $5MinutesAgo -InstanceID 1020)[0]

$DHCPLowEventMessage = $DHCPLowEvent.Message

$DHCPLowPercent = 100 – $DHCPLowEvent.ReplacementStrings[1]

If ($DHCPLowPercent -le $DHCPLowPercentThreshold){

$messageParameters = @{

Subject = “Warning: DHCP Addresses Low”

Body = “$DHCPLowEventMessage”

From = $FromAddress

To = $ToAddress

SmtpServer = $SmtpServerAddress


Send-MailMessage @messageParameters



AD user DirectReports

Get-ADUser chadmin -Properties * | Select-Object -ExpandProperty DirectReports

Note : chadmin is user name

%d bloggers like this: