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 V3 – Finding What’s New?

So I’ve started to play around with PowerShell V3 and outside of some neat IDE stuff in ISE, where do I get started? Well I guess the best spot is to start with what new cmdlets have been added? So let’s do a little comparison.

The first problem we run in to is that PowerShell V3 is an update and not a side by side install. Not a horrible problem since we can start PowerShell in compatibility mode

PowerShell -Version 2

This will only work with the console and not ISE. So lets load that up and do the following.

Get-Command | Export-Clixml ps2cmds.cli

Now that we’ve exported all the cmdlets (and alias and functions) from PS2 lets hop in to PS3 and compare the two.

$v2 = Import-Clixml ps2cmds.cli

$v3 = Get-Command

#filter it to only cmdlets for now - CommandType 8 is cmdlet

$v2cmd = $v2 | where { $_.CommandType -eq 8}

$v3cmd = $v3 | where {$_.CommandType -eq 8}

$newcmds = Compare-Object $v2cmd $v3cmd -Property Name -PassThru


#should return 351, or at least it did for me

$newcmds | sort module,name | ft name,module

You’ll notice a few things when running this. First you’ll see that a lot of modules are automatically loaded now. I believe this is done because of the confusion with AD modules and people not knowing they needed to load them first. Secondly you’ll notice there are some cool new modules like CimCmdlets, PSSCheduledJob and PSWorkflow. One other thing to take a peek at is the new core PowerShell cmdlets.

$newcmds | where {$_.module -eq $null}

There are a few here that stand out, for example Get-ControlPanelItem, Unblock-File and Show-Command. The Show-Command is a pretty neat way to explore PowerShell. Basically creates a pop-up window that lets you dig through the cmdlets a little easier.

So many new things to play around with!