Skip to content

Instantly share code, notes, and snippets.

@CalvinRodo
Created January 19, 2012 18:19
Show Gist options
  • Save CalvinRodo/1641637 to your computer and use it in GitHub Desktop.
Save CalvinRodo/1641637 to your computer and use it in GitHub Desktop.
Improved Module Loading in Windows Powershell profile.
#Add the profile path to the environment path variable
$ProfileRoot = (Split-Path -Parent $MyInvocation.MyCommand.Path)
$env:path = ";$ProfileRoot"
#Load all of the modules from ./Modules/* that aren't already loaded.
(gci $ProfileRoot"/Modules") | ForEach { if ( -Not( Get-Module $_.Name ) ) { Import-Module $_.Name; Write-Host Importing Module: $_.Name } }
@CalvinRodo
Copy link
Author

Stick this in your Powershell Profile and it will automatically load all of the modules that you've stuck in you WindowsPowerShell\Modules folder.

So for example to install the foo.dll module automaticall on powershell startup put it here:
~\My Documents\WindowsPowerShell\Modules\foo\foo.dll

Then when you next load up powershell it will be automatically imported.
Also if you .source your profile then it won't try to load modules that are already there which throws an exception.

@shane0
Copy link

shane0 commented Sep 14, 2016

Pretty sure this will break existing paths, don't you mean this?

$env:path += ";$ProfileRoot"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment