Built-in Snippets
The Script Explorer also provides a collection of built-in snippets that include common PowerShell structures and commands. These predefined snippets help speed up script creation and reduce errors by providing ready-made code templates that can be inserted instantly.
Available built-in snippets
- Comment Block | block-comment – A multi-line comment.
- do-until | do-until – Runs a statement list repeatedly until a condition is met.
- do-while | do-while – Runs a statement list repeatedly as long as a condition is met.
- else | else – Defines what is done when all if and elseif conditions are false.
- elseif | elseif – Provides an alternative path when an if condition is false.
- Enum | enum – An enumeration is a distinct type that consists of a set of named labels called the enumerator list.
- for | for – Creates a loop that runs commands in a command block while a specified condition evaluates to true. A typical use of the for loop is to iterate an array of values and operate on a subset of these values.
- for-reversed | forr – Reversed for loop snippet.
- foreach | foreach – Iterate through a collection, assigning a variable to the current item on each loop rather than using the automatic variable
$PSItem
. - foreach-item | foreach-item – Quicker definition of foreach. Highlight the variable name of the collection you want to use and type ‘item’ then press Tab.
- Hashtable | hashtable – A key/value store that is very efficient for finding and retrieving data.
- Here-String | hsHere-string – Escape all text but evaluate variables.
- Here-String (Literal) | hsliteral-here-string – Escape all text literally.
- if | if – Run code blocks if a specified conditional test evaluates to true.
- PSCustomObject | pscustomobject:[PSCustomObject] – Create a custom object from a hashtable of properties.
- Region Block | region – Region block for organizing and folding your code.
- splat | splat – Use a hashtable to capture the parameters of a function and pass them concisely.
- switch | switch – Equivalent to a series of if statements but simpler. The switch statement lists each condition and an optional action. If a condition obtains, the action is performed.
- try-catch | try-catch – Attempt a block of code and if a terminating exception occurs, perform another block of code rather than terminate the program.
- try-catch-finally | try-catch-finally – Attempt a block of code and if a terminating exception occurs, perform another block of code, finally performing a final block of code regardless of the outcome.
- try-finally | try-finally – Attempt a block of code and perform an action regardless of the outcome. Useful for cleanup or disconnecting active sessions even if there was a terminating error.
- while | while – Repeatedly perform an action after verifying a condition is true first.