Appendix A. PowerShell reference

 

This appendix supplies a number of templates, syntax formats, and other useful information that you can use when creating your PowerShell scripts.

A.1. Automatic variables

There are a large number of variables that PowerShell automatically creates. The list can be seen by using

Get-Help about_Automatic_Variables

Reviewing the contents of this help file is highly recommended.

A.2. Calculated fields

Use calculated fields to create new properties or to perform calculations:

Get-WmiObject -Class Win32_OperatingSystem |
select @{Name="BootTime";
Expression={$_.ConvertToDateTime($_.LastBootUpTime)}}

A.3. Flow syntax

The if and switch statements are used to control flow:

if (<condition>){ .. statements .. }
elseif (<condition>){ .. statements .. }
else { .. statements .. }

switch (variable or pipeline) {
  value { .. statements .. }
  {expression} { .. statements .. }
  default { .. statements .. }
}

In the switch statement, use break in each statement block to prevent further processing.

A.4. Function template

The following listing provides a template for advanced functions, containing all validation options as well as comment-based help options.

A.5. Hash tables

A.6. Loops

A.7. Operators

A.8. PowerShell install folder

A.9. Size constants

A.10. Type shortcuts