Tom Talks Microsoft Teams and Microsoft 365 news and opinions

PowerShell Add a Persistent Module Path to $env:PSModulePath

One of the benefits or using or writing module in PowerShell version 3 or above is that they auto load as you need them. They do this if the modules sit in a number of predefined paths. Often you might want to add an additional persistent path to check for modules to auto load.

This information is actually held in the registry, you can see it by $env:PSModulePath or by looking in the registry

image

If you want to add a custom path that persists accross PowerShell sessions you can follow these steps running a PowerShell session as administrator:

$originalpaths = (Get-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PSModulePath).PSModulePath

# Add your new path to below after the ;

$newPath=$originalpaths+’;C:\Users\Tom\SkyDrive\PowerShell\’

Set-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PSModulePath –Value $newPath

Example:

image

after update we see the registry is updated:

image

New PowerShell Session:

image

Credit for Technique:

http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/23/use-powershell-to-modify-your-environmental-path.aspx

About the author

Tom Arbuthnot

A Microsoft MVP and Microsoft Certified Master, Tom Arbuthnot is Founder and Principal at Empowering.Cloud as well as a Solutions Director at Pure IP.

Tom stays up to date with industry developments and shares news and his opinions on his Tomtalks.blog, UC Today Microsoft Teams Podcast and email list. He is a regular speaker at events around the world.

  • Is this the best way to achieve this compared to modifying default PowerShell profile ?
    Will this registry modification be erased if the upgrade PowerShell version ?

    Thank you Tom for sharing this tip with us ;)

  • Hi Tom

    Great post as it helped me fix a broken PS / ISE on a dev server.
    I ended up editing the PSModule Path variable from Advanced Settings in Control Panel as I wasn’t convinced the changes you describe would persist following a reboot.

  • Tom Talks Microsoft Teams and Microsoft 365 news and opinions
    >