Using PowerShell Regions

Learn how to use regions in PowerShell v3.

John Savill

June 14, 2013

1 Min Read
Using PowerShell Regions

Q: What are Windows PowerShell Regions?

A: Typically, when you create a large PowerShell script, you have modules of code, and it can sometimes be very overwhelming and hard to navigate with all the code always visible.

PowerShell Regions let you place blocks of code into a collapsible "region" reducing what is displayed on screen until you actually want to see that region of code. Then you simply click to view the content of the region. To create a region use this command:

#region 

and to end it:

#endregion

For example, here is a complete region:

#region Background job$Trigger = New-JobTrigger –Daily –At 2amRegister-ScheduledJob –Name MyScheduledJob –ScriptBlock {Get-Process} –Trigger $TriggerGet-ScheduledJob(Get-ScheduledJob –Name MyScheduledJob).JobTriggersGet-ScheduledJob –Name MyScheduledJobUnregister-ScheduledJob -Name MyScheduledJob#endregion

This is what regions look like in your code. Notice all are collapsed except the Background job region. Notice the line numbers are still accurate which gives you an idea of the number of lines in each region.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like