Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Module Linking

Posted on February 27, 2024February 27, 2024

I know it has been a while since I've given the blog any attention. I am hoping to correct that in 2024, although I'm already behind schedule. But let's get to it.

I have a short tip today that you may find useful, especially if you write modules for your private use. I have a number of such modules that I have written to fill my needs. These are private modules that I don't publish to the PowerShell Gallery. I develop and maintain these modules in C:\Scripts. This means that when I need to import the module, I have to type the full path.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Import-Module -Name C:\Scripts\MyModule.psd1

Granted, I load many of these modules in my PowerShell profile script. But it would be nicer to have PowerShell auto-import them as it does other modules.

PSModulePath

The auto-import works because most modules are installed in a directory that is listed in the $env:PSModulePath environment variable. This variable is a list of directories where PowerShell looks for modules.

PS C:\> $env:PSModulePath -split ";"
C:\Users\Jeff\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\7\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files (x86)\Microsoft SQL Server\150\Tools\PowerShell\Modules

I could modify this variable to include my C:\Scripts directory, but I don't want to do that. That would add modules that are truly in development and not ready for general use.

Link It!

Instead, I can create a symbolic link to the module in one of the directories listed in $env:PSModulePath. I can do this with the New-Item cmdlet.

I have a PowerShell 7 module I use to manage my Stripe account. I maintain this module in C:\Scripts\StripeTools. Instead of manually importing the module, it would be nicer to have it auto-imported. I can create a symbolic link to the module in the C:\Program Files\PowerShell\Modules directory.

New-Item -Path "c:\program files\Powershell\Modules" -Name StripeTools -ItemType SymbolicLink -Value C:\scripts\StripeTools\

It is that simple. I can continue to maintain the module in C:\Scripts where files are automatically backed up. But I can use the module like any other. Of course, if I'm in the middle of updating something, I'll be using the module in it current state. But that's on me, and really isn't that much different than what I face now.

What is really interesting, is that C:\Scripts is itself a pointer a folder on OneDrive. This keeps my files backed up to the cloud and synchronized across my devices. When I import my linked module, I'm actually using files from my OneDrive folder. This is a big win all the way around.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “Module Linking”

  1. Mike Shepard says:
    February 27, 2024 at 10:51 am

    Awesome trick. Can’t believe I never thought of this!

  2. Pingback: Symbolic Links and Powershell Modules – Curated SQL
  3. Pingback: The PowerShell Podcast IAM in Focus: Insights from Adil Leghari – PowerShell.org
  4. Pingback: The PowerShell Podcast IAM in Focus: Insights from Adil Leghari – 247 TECH
  5. Pingback: PowerShell SnippetRace 17-18/2024 | PowerShell Usergroup Austria

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d