Ways to launch Powershell ISE

  • search “powershell ISE” or
  • type ise in powershell console
Layout:

On the left side of the screen is an interactive Windows PowerShell console. On the right side of the screen is the Commands add-on.

Commands add-on

The Commands add-on provides you the ability to build a command by using the mouse. Once you have built the command, click the Run button to copy the command to the console window and execute the command. Insert button will copy the command to the Windows Powershell console, but will not execute the command, so you still can edit the command in the Powershell console and hit enter to run it.

Multiple argument: Use a semicolon to separate the arguments.

E.g. start the notepad and calculator:

  • Select the Invoke-WmiMethod cmdlet from the list.
  • In the class block, add the WMI class name Win32_Process.
  • In the name block, enter the method name create.
  • In the argument list, enter notepad; calc

Script Pane

Pressing the down arrow beside the word script in the upper-right corner of the console pane reveals a new script pane into which you can start entering a script.

You can also use the keyboard shortcut Ctrl+N.

Just because it is called the script pane does not mean that it requires you to enable script support to use it. As long as the  file is not saved, you can enter commands that are as complex as you wish into the script pane, with script support restricted, and the code will run when you execute the script. Once the  file is saved, however, it becomes a script, and you will need to deal with the script execution policy at that point.

You can still use commands add-on with the script pane, but it requires an extra step, after modifying the command in add-on pane, click copy button, then paste it in the script pane.

IntelliSense provides pop-up help and options while you type, permitting rapid command devel- opment without requiring complete syntax knowledge.

Snippets

To start the Windows PowerShell ISE snippets, use the Ctrl+J keystroke combination (you can also use the mouse to choose Start Snippets from the Edit menu). Once the snippets appear, type the first letter of the snippet name to quickly jump to the appropriate portion of the snippets. Once you have identified the snippet you wish to use, press Enter to place the snippet at the current insertion point in your Windows PowerShell script pane.

Creating a new snippets (New-iseSnippet)

Before creating a new snippet, make sure that the script execution policy is permit. You can use Get-executionPolicy to see the current policy and use Set-ExecutionPolicy RemoteSigned if it’s not permitted.

To create a new snippet, we can use cmdlet New-iseSnippet, Only three parameters are required: Description, Text, and Title.

Note that when you execute a new-isesnippet command, the snippet will be saved to WindowsPowerShell folder in your Documents folder in title.snippets.ps1xml format first and then will load this into ISE. If the executionPolicy is restricted, you will get an error “Unable to find or access the file ….”, so make sure it’s remoteSigned before you execute the command.

  • Title: The name of the snippet is specified via the Title parameter.
  • Text: The snippet itself is typed into the Text parameter.When you want your code to appear on multiple lines, use the `r special character. Of course, doing this means your Text parameter must appear inside double quotation marks, not single quotes.
  • Description: describe the purpose or usage of your snippet.

A great way to simplify snippet creation is to place the snippet into a here-string object, and then pass that value to the New-IseSnippet cmdlet.

More about here-string

Windows PowerShell has a construction known as a here-string, a construction that lets you bypass the complexities otherwise involved in assigning a multi-line string value to a variable.

you indicate the start of a here-string by using syntax similar to this:

$x = @"

We want to assign our string value to a variable named $x; that’s why we started things off with $x = followed by @", the syntax that represents the start of our here-string. (Which, we should add, needs to be on a line all by itself.) We then indicate the end of the here-string by typing the construction "@, also on a line by itself.

In between the opening line and the closing line all we have to do is type the string value exactly the way we want it assigned; in turn, PowerShell will respect any line breaks, double and single quote marks, and blank spaces we type in.

This is also a nifty way to add comments to a script: you can type as much text as you want any way you want, and without having to comment out each and every line. Here’s a comment that uses a here-string:

$x = @"

This script demonstrates the use of "here-strings."
Anything typed between the top line of this code block
and the bottom line of this code block will be formatted
exactly as shown here.

"@

————————————————–

Verify the existing snippet: get-iseSnippet.

Removing user-defined Snippets

 There is no remove-IseSnippet cmdlet, but we can use Remove-item instead. note that the removed file will be deleted directly, not been moved to Recycle bin.

  • Delete all snippet: Get-iseSnippet | Remove-item
  • Use Where-object (? is an alias for the where-object) cmdlet to choose the snippets according to your criteria.
  • Add -whatif to pre-check your script’s result, so you know you are doing what you are expected to do.
  • Add-confirm to double check your action.