Skip to content

Instantly share code, notes, and snippets.

@rbaas293
Last active August 3, 2022 11:12
Show Gist options
  • Save rbaas293/3c54ae65fdf6c69ce7edab031d7121eb to your computer and use it in GitHub Desktop.
Save rbaas293/3c54ae65fdf6c69ce7edab031d7121eb to your computer and use it in GitHub Desktop.
A PowerShell Cheat Sheet

PowerShell Cheat Sheet

Variables

  • Always have a dollar sign ($) before them.
  • Example below:
$numberofcmdlets = (get-command).count

cmdlet's:

  • Write-Host simply writes text to the screen of the machine hosting the PowerShell session. Example below:
Write-Host "Whatever text I want, as long as it is inside double quotes."
  • But you can integrate variables with Write-Host. You just call them with the dollar sign notation and work them right into your text. Example below:
Write-Host "There are $numberofcmdlets commands available for use on this system."

Conditionals

In PowerShell, your normal conditional symbols like >, <, >=, <=, == are denoted in the table below:

Conditional PS
Greater Than -gt
Less Than -lt
Greater Than or Equal -ge
Less Than or Equal -le
Equal Too -eq
Not Equal Too ne
Case Sensitive Ex. -Ceq

Other Comparison Operarators are:

-Like -NotLike -Match -NotMatch -Contains -NotContains -In -NotIn -Replace

Looping:

  • The if/then mechanism; in PowerShell lingo, this is called the "construct."

    • If I wanted to have PowerShell display whether 10 was greater than 5. Example below:
    If (10 –gt 5)
     {
     Write-Host "Yes"
     }
  • A loop with an elseif:

    If (10 –gt 11)
    {
    Write-Host "Yes"
    } elseif (11 –gt 10)
    {
    Write-Host "This time, yes"
    }elseif (20 –gt 40)
    {
    Write-Host "Third time was a charm"
    } else {
    Write-Host "You're really terrible at math, aren’t you?"
    }

A good resource for PowerShell looping and other things can be found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment