PowerShell

Restart all Computers in a specific OU

Recently I received a request to shut down computers by room. Specifically, it was about a school where students often forget to shut down their computers. Of course this is possible with PowerShell, what else?

The Goal

First we have to retrieve all organizational units. Not only the parent ones, but also the child ones. I like it user-friendly with Out-GridView.


$room=Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Computers,OU=Schulsystem,DC=hlmw9,DC=local' -SearchScope OneLevel | Select-Object DistinguishedName | Out-GridView -PassThru -Title 'Select Computers OU to restart all Computers in that OU'

Note the Out-GridView command after the pipe. This gives us the possibility to select the OU in a kind of graphical interface.

Unbenannt.PNG

After selecting the OU, click ok. All computers in that OU will be shutdown.

The Code

Note that you have to adapt line 1 with your OU structure as you see fit.


$room=Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Workstations,DC=sid-500,DC=com' -SearchScope OneLevel | Select-Object DistinguishedName | Out-GridView -PassThru -Title 'Select Computers OU to restart all Computers in that OU'
$comp=(Get-ADComputer -SearchBase $room.distinguishedname -Filter *).Name

Foreach ($c in $comp)

{Restart-Computer -ComputerName $c -ErrorAction SilentlyContinue -Force -Verbose}

That’s it. Automation complete.

2 replies »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.