I’ve gone to The Dark Side: I took a PowerShell class. I’ve started writing new scripts and replacing old VBscripts. Was it scary? Terrifying. Learning a new language – programming or speaking – is never easy. Do I know everything? Not even close. I know how to get help (get-help – get it? harharhar…), and who to ask for help.
One of the biggest obstacles for me was figuring out the punctuation and syntax in PowerShell. How do I comment out a line? Why (), [] and {}? What does it all mean?
I made myself this PowerShell Punctuation Cheat Sheet. Hopefully it helps you out too!
Symbol | Name | Function | Example |
---|---|---|---|
# | Pound or Hash | Declare comment line. | |
$ | Dollar sign | Declare a variable. | $Name |
= | Equal | Assigns value to variable. | $Name="Jes" |
| | Pipe | Take info from first cmdlet; pass to second. | Get-Childitem | Get-Member |
– | Hyphen |
Joins verbs-nouns. Used for parameters, modifiers, filters. |
Get-Member Get-Process -name s* |
" | Double-quote | Use around text. Variables will show the value. |
$a=100 "The value of a is $a" will output as: The value of a is 100 |
' | Single-quote | Treats text as literal. |
$a=100 'The value of a is $a' will output as: The value of a is $a |
` | Escape/grave accent | The escape character. Use to take the next character literally. |
"The value is `$10" will output as: The value is $10 It won't treat it as a variable. |
() | Parentheses |
Provide arguments. Grouping. |
"text".ToUpper() (2 +1)*4 |
[] | Brackets |
Access elements of array. In -like comparisons. Set variable type. |
$Names[0] -like [ab]* [int]$count |
{} | Curly brackets | Enclose block of code. | Get-Wmiobject -list | where {$_.name -match "win32*"} |
, | Comma | Separate items in a list. | |
; | Semi-colon | Run multiple commands on same line. | $Name="Jes"; $Name |
+ | Plus | Concatenate. |
There are four main commands to remember for PowerShell. Using these four commands, you can figure out nearly anything.
Command | Function | Example |
---|---|---|
Get-Help | Get help with a cmdlet. Provides name, syntax, links and more. | Get-Help Get-Date |
Get-Command | Provides information about all available cmdlets. | Get-Command Format-List |
Get-Member | Get the properties and methods of an objects. | Get-Process | Get-Member |
Get-PSDrive | Lists all the PowerShell drives in the current session – FileSystem, Functions, Alias, etc. | Get-PSDrive |
Note: I have not included the percent symbol (%) or the question mark (?) on purpose. They are most commonly used as aliases for other commands, and that is beyond the scope of this post.