I recently attended the PowerShell DeepDive at TEC2011.  It was my first time attending such an event and it was an incredible experience getting to meet with so many people from the community. One of the people I got to meet was Lee Holmes from the PowerShell team at Microsoft.  It was truly a pleasure speaking with the different people at the DeepDive because it felt like talking with regular everyday people, even though I look up to a lot of these people in the same way normal people might look at movie stars or other famous people.  The people from the PowerShell community are my superstars ;-)  Lee was no exception to this, it was totally awesome talking with him.

When speaking with Lee, we got to talking about what I use PowerShell for and a script I’ve been working on at work.  He made a suggestion to me to split each tab in my interface into it’s own file to help manage the code better.  I just got that done and posted about it on Twitter and people were wanting an example of what I meant when I said I split my tabs up into files so I threw something really simple together.

This is essentially how I had been doing my tabs, with all the code for each tab under the tab. Works fine with a little 46 line script, but once you get to 800 lines or so, it becomes crazy keeping up with stuff and scrolling all over the place even when you know where you’re going.

Import-Module PowerBoots

New-BootsWindow -Title “Tabs” -Width 640 -Height 400 `
-ResizeMode “NoResize” -WindowStartupLocation “CenterScreen” {
Grid -Children {
TabControl -Name “TabControl” -Margin “8,8,8,39″ -Opacity 0.9 {

#################################################
### TAB 1
#################################################

. .\Tabs\Tab1.ps1

#################################################
### TAB 2
#################################################

. .\Tabs\Tab2.ps1

} | tee -var global:TabControl # END TAB CONTROL

Button `
-Name “btnExit” -Content “E_xit” `
-Height 32 -Width 90 `
-Margin “0,0,8,3″ -Padding 0 `
-HorizontalAlignment “Right” -VerticalAlignment “Bottom” `
-On_Click({
$BootsWindow.Close()
}) | tee -var global:btnExit
} # END MAIN GRID
}

This is the new slimmed down script, only 30 lines. Each tab has now been stripped out and put in individual files within a “tabs” directory with the main script.

This keeps things nice and neat, and best of all, easier to manage when I need to work on stuff.

TabItem -Name “Tab1″ -Header “TestTab1″ -IsEnabled:$True {
Grid -children {
TextBlock `
-Name “lblTest” `
-Margin “8,8,8,8″ `
-Text “Hello” `
| tee -var global:lblText
} # END TAB1 GRID
} | tee -var global:Tab1 # END TAB1

Here’s one tab for an example. I simple dot source the tab scripts and it all works as would be expected. I don’t know that this is the best way to do it, but it works :-)  Always learning more and always open to new or alternate ways of doing things.

Note that you’ll need to get Jaykul’s PowerBoots Module for this to work. You can grab that here http://showui.codeplex.com/