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 | <th>
Name
</th>
<th>
Function
</th>
<th>
Example
</th>
</tr>
<tr>
<td>
#
</td>
<td>
Pound or Hash
</td>
<td>
Declare comment line.
</td>
<td>
</td>
</tr>
<tr>
<td>
$
</td>
<td>
Dollar sign
</td>
<td>
Declare a variable.
</td>
<td>
$Name
</td>
</tr>
<tr>
<td>
=
</td>
<td>
Equal
</td>
<td>
Assigns value to variable.
</td>
<td>
$Name="Jes"
</td>
</tr>
<tr>
<td>
|
</td>
<td>
Pipe
</td>
<td>
Take info from first cmdlet; pass to second.
</td>
<td>
Get-Childitem | Get-Member
</td>
</tr>
<tr>
<td>
–
</td>
<td>
Hyphen
</td>
<td>
Joins verbs-nouns.<br /> Used for parameters, modifiers, filters.
</td>
<td>
Get-Member<br /> Get-Process -name s*
</td>
</tr>
<tr>
<td>
"
</td>
<td>
Double-quote
</td>
<td>
Use around text. Variables will show the value.
</td>
<td>
$a=100<br /> "The value of a is $a" will output as:<br /> The value of a is 100
</td>
</tr>
<tr>
<td>
'
</td>
<td>
Single-quote
</td>
<td>
Treats text as literal.
</td>
<td>
$a=100<br /> 'The value of a is $a' will output as:<br /> The value of a is $a
</td>
</tr>
<tr>
<td>
`
</td>
<td>
Escape/grave accent
</td>
<td>
The escape character. Use to take the next character literally.
</td>
<td>
"The value is `$10" will output as:<br /> The value is $10<br /> It won't treat it as a variable.
</td>
</tr>
<tr>
<td>
()
</td>
<td>
Parentheses
</td>
<td>
Provide arguments.<br /> Grouping.
</td>
<td>
"text".ToUpper()<br /> (2 +1)*4
</td>
</tr>
<tr>
<td>
[]
</td>
<td>
Brackets
</td>
<td>
Access elements of array.<br /> In -like comparisons.<br /> Set variable type.
</td>
<td>
$Names[0]<br /> -like [ab]*<br /> [int]$count
</td>
</tr>
<tr>
<td>
{}
</td>
<td>
Curly brackets
</td>
<td>
Enclose block of code.
</td>
<td>
Get-Wmiobject -list | where {$_.name -match "win32*"}
</td>
</tr>
<tr>
<td>
,
</td>
<td>
Comma
</td>
<td>
Separate items in a list.
</td>
<td>
</td>
</tr>
<tr>
<td>
;
</td>
<td>
Semi-colon
</td>
<td>
Run multiple commands on same line.
</td>
<td>
$Name="Jes"; $Name
</td>
</tr>
<tr>
<td>
+
</td>
<td>
Plus
</td>
<td>
</td>
<td>
Concatenate.
</td>
</tr>
---|
There are four main commands to remember for PowerShell. Using these four commands, you can figure out nearly anything.
Command | <th>
Function
</th>
<th>
Example
</th>
</tr>
<tr>
<td>
Get-Help
</td>
<td>
Get help with a cmdlet. Provides name, syntax, links and more.
</td>
<td>
Get-Help Get-Date
</td>
</tr>
<tr>
<td>
Get-Command
</td>
<td>
Provides information about all available cmdlets.
</td>
<td>
Get-Command Format-List
</td>
</tr>
<tr>
<td>
Get-Member
</td>
<td>
Get the properties and methods of an objects.
</td>
<td>
Get-Process | Get-Member
</td>
</tr>
<tr>
<td>
Get-PSDrive
</td>
<td>
Lists all the PowerShell drives in the current session – FileSystem, Functions, Alias, etc.
</td>
<td>
Get-PSDrive
</td>
</tr>
---|
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.
About Jes Borland
Jes Borland is a Premier Field Engineer - SQL Server for Microsoft, where she is a trusted technical adviser, deep technical support, and teacher for her customers. Her experience as a DBA, consultant, and 5-time Data Platform MVP allow her to help the business and the IT teams reach their goals. She has worked with SQL Server as a developer, report writer, DBA, and consultant. Her favorite topics are administration, automation, and performance. She frequently presents at user groups, SQL Saturdays, and other community events. She is also an avid runner and chef.
Social Sitings