powershell

This is an old revision of the document!


Powershell

Interactive Mode

  • Use h or history command to list history
    • Use #<num> <tab> to recall the command to the command prompt without executing it
    • Use Invoke-History <num> or r <num> to execute the specified command
    • Open stored history in text editor.
      • notepad (Get-PSReadlineOption).HistorySavePath
  • Use get-history | out-gridview -Passthru | invoke-history to get and invoke command from history using a grid view. Alternatively, h | ogv -p | r. Or, just create a function for it, function hh {get-history | out-gridview -Passthru | invoke-history}

Operators

Variables

$fname = "Spunky"
$lname = "Violet"
 
Write-Host "World meet $fname $lname."
  • $?, exit code of last operation
  • $ARGS, array of arguments passed to script.
  • $NULL, variable that represents null value, big surprise.

Conditionals

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

  • -executionpolicy bypass, overrrides local system policy configuration to allow any script to run. Sometimes you just need to get things done.
powershell -executionpolicy bypass -command <command text>
  • powershell.1642271051.txt.gz
  • Last modified: 2022/01/15 18:24
  • by mgupton