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.

Users

Domain (NetBIOS) Name
$env:userdomain

Current Username
[Environment]::UserName

List all local administrators on current computer
net localgroup administrators

Computers

Computer Name
$env:computername

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

Files/Text

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
‘n

Messagebox Buttons
[System.Windows.Forms.MessageBoxButtons]::YesNo

Click here for more details

Launch a messagebox with a system icon i.e. a warning or question
[System.Windows.Forms.MessageBoxIcon]::Question)

Click here for more details

Misc.

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”
pause

List approved verbs to use with your own Functions
get-verb

Get the variable type
$a.GetType();

Advertisements