Blog Archives

PowerShell: Working with Modules in ISE

Now that ISE isn’t crap with V3 I find that I dont really use any other editors. The one problem I have is when I work with modules, so I did a little screwing around and came up with this.

Function Import-CurrentFileAsModule




#get paths

$filePath = $psise.CurrentFile.FullPath

$folder = split-path $filePath

#save if not already saved

if($psise.CurrentFile.IsUntitled){Write-Error "Must save file first! Sorry didn't feel like implementing the dialog box!" -ErrorAction Stop}

if(-not $psise.CurrentFile.IsSaved){$psise.CurrentFile.Save()}

$global:WorkingModule = $null

#import the folder or the file if its standalone

$psise.CurrentPowerShellTab.files | ?{-not $_.issaved} |? {$_.fullpath -like "$folder*"} | %{$}
$Global:WorkingModule = Import-Module $folder -Force -ErrorAction Stop -PassThru -Verbose:$false | select -ExpandProperty name

catch{$folderFailed = $true}



try {Import-Module $filePath -Force -ErrorAction Stop -Verbose:$false}

catch{ write-error "Not a module file!" -ErrorAction Stop}


##post processing

if(Test-Path function:\PostModuleProcess)


Write-Verbose "Processing PostModuleProcess Function"





Write-Verbose "--Create a PostModuleProcess function to excute code after import--"


Write-Verbose "Remove -verbose tag from last cmd in this file to stop verbose messaging"



Function Get-ModuleVariable{






&(gmo $global:workingmodule){Get-Variable -Name $args[0] -Scope script -ValueOnly} $name




&(gmo $global:workingmodule){Get-Variable -Scope script -ValueOnly}




Function Set-ModuleVariable{









&(gmo $global:workingmodule){Set-Variable -Name $args[0] -Value $args[1] -Scope script} $Name $Value



$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Reload Module',{Import-CurrentFileAsModule -verbose},"F6")

,{Import-CurrentFileAsModule -verbose},"F6")

Just dump this in your profile or in a file and load it in your profile and F6 is ready to be used!

This create a couple of functions to allow you to look in to and edit module (script:  scope) variables, Get/Set-ModuleVariable and it will run PostModuleProcess function (if you create it) so that if you want to run any code after it loads you can easily do that.

You’ll notice in that last line that the Import-CurrentFileAsModule has the verbose tag on it, once you’re comfortable using it you can remove that.

Let me know what you think!

P.s. Thanks for the tips Jaykul!

opps! completely forgot that I should save all files in the module!

Powershell ISE History Window (why no F7 for ISE?!)

So with a classic command prompt and powershell console you can hit F7 to bring up a history window that will allow you to cycle through past commands easily. Sure you can use the Up and Down key there as well as with ISE but there is no history window for ISE. With all the cool things they’ve done with it, I’m kind of surprised.

So, I’ve come up with a simple little function to create this feature..

function Copy-HistoryItem{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$fg = $psise.Options.OutputPaneForegroundColor
$bg = $psise.Options.OutputPaneBackgroundColor
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "History"
$objForm.Size = New-Object System.Drawing.Size(300,300)
$objForm.StartPosition = "CenterScreen"
$objForm.FormBorderStyle = "SizableToolWindow"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")

$objForm.Add_Resize({$objlb.Size = New-Object System.Drawing.Size(($objForm.width-15),$objForm.height) })

$objlb = New-Object System.Windows.Forms.ListBox
$objlb.Location = New-Object System.Drawing.Size(0,0)
$objlb.Size = New-Object System.Drawing.Size(($objForm.width-15),$objForm.height)
$objlb.backcolor= [system.drawing.color]::fromargb($bg.a, $bg.r, $bg.g, $bg.b)
$objlb.forecolor = [system.drawing.color]::fromargb($fg.a, $fg.r, $fg.g, $fg.b)

get-history | foreach {$objlb.items.add($_.commandline)} | out-null

$objForm.Topmost = $True

[void] $objForm.ShowDialog()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(‘Show history’,{Copy-HistoryItem},"F7")


This even adds the ability to hit the F7 key to bring up the window.

You can resize the window to your liking and just double click on the item you want and it will place the command in the command window for you.

This was inspired by some code i received by a Community member, Kartek Bielawski, over in the MS Forums for Powershell. Most of the code was taken from a Technet Powershell Tip of the Week post on how to use forms.

Powershell ISE Color Customizer

NOTE: PowerShell V3 has its own color editor now!

I created a simple color customizer that can be found over here

This is version 1, and if I find there is enough interest (by posts here) I’ll gladly add to it. Already have some ideas on things I can do to make this way cooler, but have wasted enough time on something I’ll really only use once :)

Let me know what you think and if you think its a useful tool or if you have any idea’s you’d like to see added.



Things I could add:

  • predefined themes (if you come up with a cool color scheme let me see it!)
  • better color picker using something like one of these
  • add other things like font face and type ( I suggest you use Verdana)
  • other nifty things like setting multiple items at once, picking the color by selecting the object.. etc etc