PowerShell Scripts


Druidsys.com's collection of Windows PowerShell Scripts.

All Active Directory computers


$dom = 'LDAP://DC=yourdomainname;DC=local'
$root = New-Object DirectoryServices.DirectoryEntry $dom

$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root
$selector.set_pagesize(1000)
$adobj= $selector.findall() | where { $_.properties.objectcategory -match "CN=Computer" }

foreach ($computer in $adobj)
{

# Do what you want with each machine

}

Get external IP



# Name: externalip.ps1
# Function: Get your external IP address
# Language: Windows PowerShell
# Author: Jimmy Johansson
# Creation: 2009-08-31
# Example: "./externalip" will return your external IP

Save the script in a text document and save it with the ps1 extension and then call it from a PowerShell prompt.


$client = new-object system.net.WebClient $client.Headers.Add("user-agent", "PowerShell") $data = $client.OpenRead("http://icanhazip.com") $reader = new-object system.io.StreamReader $data [string] $s = $reader.ReadToEnd() write-host "Your external IP is $s" $data.Close() $reader.Close()

List locked out accounts in Active Directory


write-host
write-host "Locked out accounts"
write-host "###################"

$dn = "dc=$args[0],dc=druidsys,dc=com" #replace with your domain
$domain = "LDAP://$dn"
$root = New-Object System.DirectoryServices.DirectoryEntry $domain
$query = new-Object System.DirectoryServices.DirectorySearcher
$query.searchroot = $root
$objClass = $query.findall() | Where-Object {$_.properties.objectclass -eq "user" -and $_.properties.lockouttime -ge 1}

foreach ($user in $objClass) {

$prop = $user.properties
write-host $prop.samaccountname
}

write-host

Reboot a windows machine remotely



# Name: reboot.ps1
# Function: Enter a computername to reboot it, and monitor the process
# Language: Windows PowerShell
# Author: Jimmy Johansson
# Creation: 2008-11-16
# Example: "./reboot.ps1 webserver1" will reboot the machine webserver1
# It must be a windows machine and you must have the required permissions to reboot it

Save the script in a text document and save it with the ps1 extension and then call it from a PowerShell prompt.


$buffer = ping $args[0] | findstr "Reply" if ($buffer -match "Reply*") { write-host write-host "Rebooting $target..." (gwmi Win32_OperatingSystem -computer $args[0]).reboot() | out-null write-host write-host "The machine is now monitored until it is back online." write-host "Press [Ctrl-C] to stop this monitor." write-host write-host "Waiting for power off..." -nonewline while ($buffer -match "Reply*") { $buffer = ping -n 1 $args[0] | findstr "Reply" } write-host "done." write-host "Waiting for system to come back online..." -nonewline $buffer = ping $args[0] | findstr "Request" while ($buffer -match "Request*") { $buffer = ping -n 1 $args[0] | findstr "Request" } write-host "done." write-host write-host "Reboot completed." write-host } else { write-host $target "does not respond." } write-host

Tone to frequency converter


# Name: tone2hz.ps1
# Function: Enter a tone with an octave and it will give you the frequency back
# Language: Windows PowerShell
# Author: Jimmy Johansson
# Creation: 2008-07-30
# Example: "./tone2hz A# 5" will give you the frequency for A sharp in the fifth octave.
# If no octave is given, the fourth is assumed, octaves are valid from 2 to 7.

Save the script in a text document and save it with the ps1 extension and then call it from a PowerShell prompt.


$args[0] = $args[0].ToUpper() if ($args[0][0] -eq "A") { if ($args[0][1] -eq "#") { $n = 1 } elseif ($args[0][1] -eq "B") { $n = -1 } else { $n = 0 } } if ($args[0][0] -eq "B") { if ($args[0][1] -eq "B") { $n = 1 } else { $n = 2 } } if ($args[0][0] -eq "C") { if ($args[0][1] -eq "#") { $n = -8 } else { $n = -9 } } if ($args[0][0] -eq "D") { if ($args[0][1] -eq "#") { $n = -6 } elseif ($args[0][1] -eq "B") { $n = -8 } else { $n = -7 } } if ($args[0][0] -eq "E") { if ($args[0][1] -eq "B") { $n = -6 } else { $n = -5 } } if ($args[0][0] -eq "F") { if ($args[0][1] -eq "#") { $n = -3 } else { $n = -4 } } if ($args[0][0] -eq "G") { if ($args[0][1] -eq "#") { $n = -1 } elseif ($args[0][1] -eq "B") { $n = -3 } else { $n = -2 } } $p = $n / 12 $hz = [math]::round(([math]::pow(2,$p)) * 440, 3) if ($args[1] -le 1) { $oct = 4 } else { $oct = $args[1] } if ($oct -ne 4) { if ($oct -le 3) { $hz = $hz / 2 } if ($oct -le 2) { $hz = $hz / 2 } if ($oct -ge 5) { $hz = $hz * 2 } if ($oct -ge 6) { $hz = $hz * 2 } if ($oct -ge 7) { $hz = $hz * 2 } } write-host write-host "$hz Hz (Octave: $oct)" write-host [console]::beep($hz,500)