Autoloading Powershell Modules

No Comments April 13, 2011

In a previous post I showed a simple module to check the validity of XML documents from a Powershell command line. Over the past few months I’ve found this little function to be incredibly useful, enabling me to hunt down some odd errors that were being generated as part of our continuous integration builds and a few spurious errors in our live environments.

However, I was getting annoyed having to remember to import the module into my Powershell session every time I wanted to call the function – so I knocked together a simple autoloader script to handle this for me:

function Autoload {
    $private:AutoLoad = @{

foreach ($private:module in $AutoLoad.Modules) {
    Import-Module $module -EA SilentlyContinue

$private:arr = $env:path.Split(';') | Select -Unique
foreach ($private:path in $private:AutoLoad.Paths) {
    if ($private:arr -notcontains $private:path) {
        $private:arr += $private:path
$env:path = [String]::Join(";", $private:arr)






By changing the Modules array I can force Powershell to automatically load the modules I’m interested in with every console session. In reality, this array is stored as an XML document alongside my Microsoft.PowerShell_profile.ps1 file, but I’ll leave that as an exercise for the reader to implement. The second part of the function allows me to specify paths that I want to add to the environment path variable for each session.

No Comments