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.
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.
Awesome trick. Can’t believe I never thought of this!