PowerShell Script to run commands per Active Directory OU

I regularly run into a case in which it is handy to have a script to hand to run against a group of windows desktops or servers in an Active Directory OU.

Requirements to run the below are below.

  1. WinRM needs to be running on the relevant desktops and servers (can be completed by GPO) or by running “winrm quickconfig” in a PowerShell session on the machine
  2. Remote Server Admin Tools need to be installed on the desktop or server in which you are running the script (not required on DC’s)

The script is broken down below.

Import-Module ActiveDirectory

Import the AD module (RSAT requirement)

# OU Name
$OU = "OU=SETOFCOMPUTERS,OU=COMPUTEROU,DC=DOMAINNAME,DC=COM"

The $OU variable holds the full LDAP filter of the targeted OU

$Script = "ipconfig /flushdns"

The $Script variable holds the command to which you would want to run against the computers. (installations, batch scripts or any other commands)

# Window Title
$Host.UI.RawUI.WindowTitle = "Processing Computers in OU " + $OU

# Connectivity Timeout
$timeoutSeconds = 20

The window title of the PowerShell windows will display “Processing Computers in OU OU=SETOFCOMPUTERS,OU=COMPUTEROU,DC=DOMAINNAME,DC=COM” while the Connectivity Timeout variable is used later to complete inital connectivity of the computer before completing the script.

$ComputerNames = Get-ADComputer -Filter * -SearchBase "$OU" | Select Name

The $ComputerNames variable uses the AD command Get-ADComputer with the filter of the $OU variable to select all computers in the targeted OU.

FOREACH ($Computer in $ComputerNames) {
    if(Test-Connection -ComputerName $($Computer).Name -Count 1 -TimeToLive $timeoutSeconds -ErrorAction 0){
	Write-Host $Computer.Name -ForegroundColor Green 
        Invoke-command -COMPUTER $Computer.Name -ScriptBlock {'$Script'}
    }
    else {Write-Host "Computer NOT FOUND $Computer.Name" -Foreground Red
    }

}

The foreach loop runs a test-connection or ping with a TTL of 20 seconds, if this fails the “Computer Not Found COMPUTERNAME” message will be returned. If successful then the invoke-command will run a remote PowerShell session to execute the $Script variable on the targeted desktop.

Enjoy.

Full Code:

Import-Module ActiveDirectory

# OU Name
$OU = "OU=SETOFCOMPUTERS,OU=COMPUTEROU,DC=DOMAINNAME,DC=COM"

#Script to run on each computer
$Script = "ipconfig /flushdns"

# Window Title
$Host.UI.RawUI.WindowTitle = "Processing Computers in OU " + $OU

# Connectivity Timeout
$timeoutSeconds = 20
The window title of the PowerShell windows will display "Processing Computers in OU OU=SETOFCOMPUTERS,OU=COMPUTEROU,DC=DOMAINNAME,DC=COM" while the Connectivity Timeout variable is used later to complete inital connectivity of the computer before completing the script.

# Computer name list
$ComputerNames = Get-ADComputer -Filter * -SearchBase $OU | Select Name

# ForEach loop to complete command on each Computer
FOREACH ($Computer in $ComputerNames) {
    if(Test-Connection -ComputerName $($Computer).Name -Count 1 -TimeToLive $timeoutSeconds -ErrorAction 0){
	
    Write-Host $Computer.Name -ForegroundColor Green 
    Invoke-command -COMPUTER $Computer.Name -ScriptBlock {'$Script'}
    
    }
    else {Write-Host "Computer NOT FOUND $Computer.Name" -Foreground Red
    }

}

 

2 thoughts on “PowerShell Script to run commands per Active Directory OU

Leave a Reply

Your email address will not be published. Required fields are marked *