PowerShell Scripting

This page collates all the various scripting elements I have used ranging from how to create interactive messageboxes to searching text files for specific text.


Checks if the current user/session is running as an Adminstrator
(([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”))

Domain (NetBIOS) Name

Current Username

List all local administrators on current computer
net localgroup administrators


Computer Name

Add $ to the end of the computer name (needed for adding the computer to a group as computer samAccountName must end with a $
$comp = $env:computername
$comp = $comp + “$”

CPU Architecture (returns either “32-bit” or “64-bit”
(Get-WmiObject Win32_OperatingSystem).OSArchitecture

If ($cpu -eq “64-bit”) {}

OS Version (Example “Microsoft Windows 8.1 Enterprise”)
(get-wmiobject win32_operatingsystem).Caption


Test if a folder/file (path) exists using TEST-PATH
$path =”C:\Program Files\Microsoft\MDOP MBAM\MBAMClientUI.exe”

If (test-path $path) {}

Launch an .exe file
Invoke-Item “C:\Program Files\Microsoft\MDOP MBAM\MBAMClientUI.exe”

Dialogue Boxes

Load the .NET assemblies required to show message boxes
Add-Type –AssemblyName System.Windows.Forms

Full example
$OUTPUT= [System.Windows.Forms.MessageBox]::Show(“You are about to install some software. `n`nDo you want to continue?”,”Software Installation”,[System.Windows.Forms.MessageBoxButtons]::YesNo,[System.Windows.Forms.MessageBoxIcon]::Question)

Line break in messageboxes

Messagebox Buttons

Click here for more details

Launch a messagebox with a system icon i.e. a warning or question

Click here for more details


Get .NET 3.5 version installed
(Get-ItemProperty ‘HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5’  -Name Version).Version

Run a DOS command in PowerShell (use cmd.exe /c)
EXAMPLE: cmd.exe /c “WMIC ComputerSystem where Name=’$computer'”

Place a pause in the script running (in seconds)
Start-Sleep -s 3

Launch a PS1 script via a batch script when PS execution policy is set to restricted
PowerShell.exe -ExecutionPolicy Bypass -NoProfile -Command “\\svr\share\script.ps1”

List approved verbs to use with your own Functions

Get the variable type