Table of Contents

Powershell

Interactive Mode

History

Operators

Variables

Basic Scalar Example

$fname = "Spunky"
$lname = "Violet"
 
Write-Host "World meet $fname $lname."

Built-in/Automatic/Special Variables

Input/Output

Example of using Out-GridView for handy way to get output

Get-Alias | Out-GridView

Example of selecting a property from objects

$files = ls .\* -Exclude @("*.tmp")
 
$files | select -Property FullName 
 
# Alternative: this method does not include the property header that is included with the Select-Object method above
$files | foreach {$_.FullName}

Conditionals

If Statement

if (Test-Path $file -eq $True)
{
    Write-Host "Found a file."
}
else
{
    Write-Host "No file found."
}
if ($a -gt 2)
{
    Write-Host "The value $a is greater than 2."
}
elseif ($a -eq 2)
{
    Write-Host "The value $a is equal to 2."
}
else
{
    Write-Host "The value $a is less than 2 or was not created
or initialized."
}

Miscellanea

Allow all scripts to be ran

Set-ExecutionPolicy bypass

One-liner

powershell -executionpolicy bypass -command <command text>

Related