ISE is cool! :)

I’ve seen many interesting things done with PowerShell ISE, so I decided to play with it myself. One thing I don’t like about any powershell host: if you Import-Module or Add-PsSnapin to session in $profile it usually takes some time before you can do anything. And from time to time you do not really need those modules/ snapins. So what could one do? Well, because of ISE functionality you can have few cosole-tabs (as far as I can tell up to 8). So my idea was to create 2 powershell tabs on start: one ‘empty’, and next one – with everything loaded, and switch to first one while second is loading ‘stuff’. After that I decided that it would be cool to have an option to switch between tabs. At first I’ve just created functions, f#, that would move me to tab no #. But that was not enough for me – so I played a bit with ISE menus and got Alt + # shortcuts for each new tab, together with menu option to switch to different tabs. Whole $profile can be seen below:

PowerShell, using GeSHi
  1. $tabs = $psISE.PowerShellTabs
  2. $curtab = $tabs[$tabs.Count1]
  3. $ISEOpt = $psISE.Options
  4. $ISEOpt.OutputPaneBackgroundColor = ‘black’
  5. $ISEOpt.OutputPaneTextBackgroundColor = ‘black’
  6. $ISEOpt.OutputPaneForegroundColor = ‘white’
  7. $TabsMenu = $curtab.AddOnsMenu.Submenus.Add(‘Tabs’,$null,$null)
  8. if ($curtab -eq $tabs[0]) {
  9. $curtab.DisplayName = ‘NoProfile’
  10. $newTab = $tabs.Add()
  11. } else {
  12. $curtab.DisplayName = “PoSh # $($tabs.IndexOf($curtab))”
  13. $NewTabIndex = $tabs.Count
  14. Invoke-Expression `$SwitchToMe = { `$tabs.SetSelectedPowerShellTab(`$tabs[$NewTabIndex-1]) }”
  15. for ($i = 0; $i -lt $tabs.Count 1; $i++) {
  16. if ($NewTabIndex -le 9) {
  17. Invoke-Expression `$SwitchTo = { `$tabs.SetSelectedPowerShellTab(`$tabs[$i]) }”
  18. $MyMenu = $tabs[$i].AddOnsMenu.Submenus | Where { $_.DisplayName -eq ‘Tabs’ }
  19. [void] $Mymenu.Submenus.Add($curtab.DisplayName, $SwitchToMe ,“ALT + $NewTabIndex”)
  20. } else {
  21. # That should not happen cause limit is 8
  22. Write-Host -ForegroundColor Red “Can not add this tab to menu – move to it using CTRL + TAB”
  23. }
  24. [void] $TabsMenu.Submenus.Add($tabs[$i].DisplayName, $Switchto, “ALT + $($i+1)”)
  25. }
  26. $tabs.SetSelectedPowerShellTab($tabs[0])
  27. Add-PsSnapin Quest*
  28. ImportModule @(‘WPK’,‘IsePack’)
  29. . “C:\Program Files\Quest Software\Management Shell for ADqsft.ps1”
  30. }

It also changes default colours in OutPutPane to black background – white foreground.

The main problem I had was with “action” part of Submenus.Add method – it takes scriptblock, but I was not able to force it to expand variables, and got unpredictable results. Once I used Invoke-Expression to get proper [scriptblock] variable it all went fine. Two things I would like to fix:

* when I press alt + # I get method displayed – would love to make it ‘silent’

* if I close some tab and add next it all get messed up (two tabs with same name if it was in a middle, errors when adding new tab…)

But it does most of things I needed, and now I get my prompt very quick and if I need WPK, IsePack or Quest cmdlets – all I need to do is switch to second tab. 🙂 ISE rocks. 😀


1 thought on “ISE is cool! :)

  1. Pingback: What I DON’T like about PowerShell. « Bartek B – IT Pro PowerShell experience

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s