Editing scripts
From PowerGUI Wiki
Contents |
Syntax Highlighting
In the PowerGUI Script Editor, the following will be highlighted in specific colors to help identify those tokens more easily in a script:
- Cmdlet names (for example, Get-Command, Get-Help)
- alias names (such as gci, gcm)
- statement identifiers (for example, if, elseif, else, for, while, foreach)
- variables (for example, $ConfirmActionPreference)
- .NET Types
- comments
- quoted strings
Intellisense
The PowerGUI Script Editor supports Intellisense for
- cmdlets
- parameters
- WMI objects
- .NET objects
- variables
Intellisense is activated automatically when certain keys are pressed, such as when you press the dash (-) key after typing in Get.
Note: Intellisense can also be activated manually by pressing Ctrl+Space or through the Complete Word item in the Edit menu and button on the toolbar.
For WMI Intellisense, when using a WMI class that is not part of the default namespace, it is recommended that you enter the namespace parameter first and then the class parameter. That way, Intellisense shows you the list of classes available for the namespace you selected. For example:
Type Get-WmiObject –namespace <select your namespace> -class <select the class in that namespace>
Code Snippets
Snippets are small PowerShell code templates that include tooltips indicating what the various customizable parts of the snippet are used for.
PowerGUI supports insertion of PowerShell snippets in both the standalone Script Editor and the embedded editor in the PowerGUI admin console.
To insert a snippet
• Select Edit | Insert Snippet or press Ctrl+I.
To create a custom snippet
• Navigate to C:\Program Files\PowerGUI\snippets (or wherever you have PowerGUI installed). Create a copy of an existing snippet file and edit as desired.
Block Indent/Unindent
Within the script editor, you can indent or unindent a block of code by selecting the lines you want to change and then selecting the Edit | Advanced | Increase Indent or the Edit | Advanced | Decrease Indent menu option, accordingly.
You can also use the following shortcut keys:
• To indent the selected block of code, press Tab.
• To unindent the selected block of code, press Shift + Tab.
Block Comment/Uncomment
Within the script editor, you can comment out a block of code by selecting the lines you want to change and then selecting the Edit | Advanced | Comment Block or the Edit | Advanced | Uncomment Block menu option, accordingly.
You can also use the following shortcut keys:
• To comment out the selected block of code, press Ctrl + Shift + C.
• To uncomment the selected block of code, press Crtl + Shift + U.
Bookmarks
Within the script editor you can set bookmarks to mark the places in your script that you are working on. This facilitates making modifications in multiple places in large files when you cannot see the entire script on a page or two.
The bookmark controls are found on the Edit.Bookmarks menu. They are not located in the Tool bar by default (but can be added) and consist of the following:
- Toogle Bookmark
- Previous Bookmark
- Next Bookmark
- Clear Bookmarks
To add bookmark controls to a toolbar
- Open the Tools.Customize... menu.
- If desired, on the "Toolbars" tab, create a new toolbar.
- On the "Commands" tab, in the "Categories:" frame, click "EditCommand."
- In the "Commands:" frame, scroll to the desired bookmark command as listed above, click the command, and drag to the desired toolbar.
To turn a bookmark on or off for a given line of script
- Place your cursor on the line and then click on the Toggle Bookmark button.
To navigate between bookmarks
- Click on the Previous Bookmark or Next Bookmark buttons.
To remove all bookmarks from your script
- Click on the Clear Bookmarks button.
Bookmarks are only active for the current file that you are working on during your current editing session. If you close your file, your bookmarks will be cleared.
Code Folding / Outlining
The script editor supports code folding/outlining and #regions. By default, the editor imposes regions for if, elseif, else, comment blocks, loops, script blocks, and multi-line strings. User-defined regions can be created by using the #region / #endregion construction, as in the example below.
#region - Print out current username
#here's a comment block
#here's line 2 of the comment block
#here's line 3 of the comment block
if (1 -eq 1) {
Write-Host $env:username
}
elseif (1 -eq 2) {
Write-Host "This code should never execute"
}
else {
Write-Host "This code should never execute"
}
#endregion - Print out current username
Menu commands (no default accelerator keys) can be found on the Edit.Advanced menu.
