Skip to content

Instantly share code, notes, and snippets.

@nickadam
Created January 28, 2022 19:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickadam/ffbbd67aad81cd17b7baa21065984454 to your computer and use it in GitHub Desktop.
Save nickadam/ffbbd67aad81cd17b7baa21065984454 to your computer and use it in GitHub Desktop.
Install a powershell module locally
# Make a local repository to "publish" and "install" from
$PSRepository = (Join-Path -Path $HOME -ChildPath 'PSRepository')
mkdir $PSRepository
Register-PSRepository -Name 'local' -SourceLocation $PSRepository -PublishLocation $PSRepository -InstallationPolicy Trusted
# Make a module
mkdir .\MyModule
New-Item .\MyModule\Mine.psm1
New-ModuleManifest .\MyModule\Mine.psd1 -RootModule Mine.psm1 -Description 'MyModule'
# add functions or whatever to Mine.psm1
@"
function Get-Mine {
"This is mine!"
}
"@ | Out-File .\MyModule\Mine.psm1
# Publish the module
Publish-Module -Name '.\MyModule\Mine.psd1' -Repository 'local'
# Install the module
Install-Module -Repository 'local' Mine
# Now your functions are available all the time without the need to import
# update your modules
@"
function Get-Mine {
"This is mine!"
}
function Get-MoreMine {
"This is also mine!"
}
"@ | Out-File .\MyModule\Mine.psm1
# update mainfest
Update-ModuleManifest .\MyModule\Mine.psd1 -ModuleVersion 0.0.2
# Publish the module
Publish-Module -Name '.\MyModule\Mine.psd1' -Repository 'local'
# Install the module
Install-Module -Repository 'local' Mine -Force
@nickadam
Copy link
Author

Turns out you do not need to use a PSRepository, ModuleManifest, or even a subdirectory for the version number of the powershell modules. All that's required is the module filename sit in a subdirectory of the same name. Powershell will parse through the directories in $env:PSModulePath to resolve your cmdlets.

2022-01-30 06_40_14-Bar
2022-01-30 06_40_33-PowerShell

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