Prof. Powershell

Organize Your Scripts with PowerShell ISE 3

New to the ISE are collapsible regions, so that you don't have to scroll through dozens of lines of scripts.

The new version of the PowerShell Integrated Scripting Environment offers a number of features that I think will make it easier for script development. One of my favorites is code collapsing or regions. This is especially useful for really long blocks of code. The ISE automagically gives you this feature. Let's say you have code like this:

Function Get-Running {

Param(
[Parameter(Position=0,ValueFromPipeline=$True)]
[string]$Computername=$env:COMPUTERNAME
)

Process {
  Write-Host "Getting running services from $computername" -ForegroundColor Cyan
  $s = Get-Service -ComputerName $computername |
  where status -eq 'running'
  Write-Host "Found $($s.count) services" -ForegroundColor Cyan
  foreach ($item in $s) {
  $item.DisplayName.ToLower()
  }
} #end process

} #end function

If you open the file in Notepad or even view it here it doesn't look special. But look at the code in version 3 of the ISE in Fig. 1.

Third time's the charm for PowerShell scripting, especially with the third version of the ISE. Check out the collapsible regions.

Figure 1. Third time's the charm for PowerShell scripting, especially with the third version of the ISE. Check out the collapsible regions. (Click image to view larger version.)

Notice the small icon next to Function, Param, Process and ForEach? PowerShell automatically inserted collapsible regions whenever it detected an open parenthesis or curly brace. You can click on the icon, actually a minus symbol, to collapse that region as I did in Fig. 2.

Tada! Collapsed. It can also be toggled to expand.

Figure 2. Tada! Collapsed. It can also be toggled to expand. (Click image to view larger version.)

This makes it much easier to look at you script by "hiding" code you aren't working on. It also is a handy way of making sure you have a closing parenthesis or brace. You can collapse a section and verify you don't have any "extra" code where you aren't expecting it.

You can also add your own regions with special comment lines. Use #region at the start of your section and #endregion at the end.

Process {
  #region Getting service information
  Write-Host "Getting running services from $computername" -ForegroundColor Cyan
  $s = Get-Service -ComputerName $computername |
  where status -eq 'running'
  #endregion
  Write-Host "Found $($s.count) services" -ForegroundColor Cyan

Again you don't see it unless you are in the ISE. I like adding a little comment so that when collapsed I can still understand what the script is doing in Fig. 3.

Even collapsed, I can tell what's up with the special comments.

Figure 2. Even collapsed, I can tell what's up with the special comments. (Click image to view larger version.)

You can toggle all regions using Ctrl+M or from the Edit menu. I also love that I can copy a collapsed region to another script file -- much easier to select one line than 100!

Finally, using regions in a larger, complex script is a great way to organize your script. You might consider laying out the regions first to help plan what you want to write. When finished, you can collapse everything and "read" your script from the region headings.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular