pspm is the management tools for PowerShell modules.
You can manage PowerShell modules npm like commands.
Testing platforms:
Windows 11 Pro x64 22H2 with PowerShell 5.1 & PowerShell 7.3.1
macOS Monterey 12.6 with PowerShell 7.3.1
You can install pspm from PowerShell Gallery.
Install-Module -Name pspm
We strongly recommend using the latest PowerShellGet module to improve compatibility and stability.
To update PowerShellGet use the following command and restart PowerShell.
Install-Module PowerShellGet -Force -AllowClobber
The command pspm install
will download a module from PSGallery.
pspm install '<Module Name>'
This will create the Modules
folder in the current directory and will download the module to that folder, then import it to the current PS session.
If you want to install specific version of the Module.
You can specify the Module name with version or semver range syntax.
pspm install '<Module Name>@<Version>' # e.g) pspm install 'Pester@4.1.0'
If you want to install the module from specific repository (like Private repository)
You can specify the PSRepository name.
(You should register repositories before. See Microsoft docs)
pspm install '@<Repository Name>/<Module Name>' # e.g) pspm install '@PrivateRepo/MyModule'
You can download modules from GitHub repos.
Just <user>/<repo-name>
or <user>/<repo-name>#<ref>
, or <user>/<repo-name>#<ref>::path/to/subdir
.
<ref>
as branch
or commit-hash
or Tag
pspm install '<user>/<repo-name>'
pspm install '<user>/<repo-name>#<ref>'
pspm install '<user>/<repo-name>#<ref>::path/to/subdir'
# e.g) pspm install 'pester/Pester#7aa9e63'
You can specify Credential
or GitHubToken
If you want to get modules from private repos.
Also, if an environment variable GITHUB_TOKEN
is present, pspm uses it as GitHub Personal Access Token.
(Priority: Credential
> GitHubToken
> GITHUB_TOKEN
)
# Authenticate with Credential (username & password)
pspm install '<user>/<private-repo-name>' -Credential (Get-Credential)
# Authenticate with Personal Access Token
# You should convert token to [SecureString]
$SecureToken = ConvertTo-SecureString '<plain-token>' -AsPlainText -Force
pspm install '<user>/<private-repo-name>' -GitHubToken $SecureToken
If you want to install multiple modules at once or manage modules as code.
You can create package.json
and run the pspm install
without module name.
If there is a package.json
file in the working directory, pspm installs all modules in the list.
About package.json
, please refer the section below.
Install a module as global.
Module will save to the $env:ProgramFiles\WindowsPowerShell\Modules
(on Windows)
pspm install '<Module Name>' -Global
pspm install '<Module Name>' -g
pspm install '<Module Name>' -Scope Global
Install a module to current user profile.
Module will save to the $env:UserProfile\WindowsPowerShell\Modules
(on Windows)
pspm install '<Module Name>' -Scope CurrentUser
When -Clean
switch specified, pspm will remove ALL modules in the Modules
folder before install process.
pspm install '<Module Name>' -Clean
# WARNING: -Clean option will remove all modules in the Modules folder, NOT just the specified.
pspm install
will import modules automatically after install.
If you don't like this behavior, Specify -NoImport
switch.
pspm install '<Module Name>' -NoImport
The command pspm update
will update modules.
pspm update '<Module Name>' # specify the name of module
pspm update # with package.json
NOTICE: The pspm update
will change package.json
to save the new version as the minimum required dependency. If you don't wanna update, use pspm update -Save:$false
Thought as below scenarios.
Example@1.0.0
is installed in the system. Example
is 1.2.0
.In this scenario. If you run the command pspm install 'Example@1.x'
, pspm will NOT update the module that because 1.0.0
is satisfied 1.x
.
But if you run pspm update 'Example@1.x'
, pspm will update the Example
to 1.2.0
.
To remove a module from your Modules
folder, use:
pspm uninstall '<Module Name>'
pspm supports the "scripts"
property of the package.json
.
pspm run '<Script Name>'
pspm run '<Script Name>' -Arguments [Object[]] #with arguments
pspm run '<Script Name>' -IfPresent #run only when the scripts exist
#aliases: pspm run-script
You can omit run
when running a script with some special words. (start
, restart
, stop
, test
)
pspm start # = pspm run start
pspm restart # = pspm run restart
pspm stop # = pspm run stop
pspm test # = pspm run test
Save package.json
that has scripts
property like below in the working directory.
"scripts": {"hello": "echo 'Hello pspm !'"}
To run Hello
script, exec the command.
PS> pspm run 'hello'
Hello pspm !
You can use custom arguments when executing scripts.
"scripts": {"name": "echo 'Your name is $args[0]'"}
PS> pspm run 'name' -Arguments 'MyName'
Your name is MyName
The package.json
that has "config" keys. You can use the object as environment variable of pspm_package_config_<key>
"scripts": {"show_port": "echo \"Port: $env:pspm_package_config_port\""},
"config" : {"port": "8080"}
PS> pspm run 'show_port'
Port: 8080
You can invoke outer PowerShell script file.
"scripts": {"ps1": ".\\script.ps1"},
If you want to run a specific script at a specific timing, then you can use a hook script.
install hook
pspm install
pspm install
update hook
pspm update
pspm update
uninstall hook
pspm uninstall
pspm uninstall
Specific script hook
pspm start
pspm restart
pspm stop
pspm test
User-defined script hook
pspm run <name>
"scripts": {
"preinstall": "echo '(preinstall) run before install'",
"install": "echo '(install) run after install'",
"postinstall": "echo '(postinstall) run after install'"
}
PS> pspm install 'Pester@4.2.0'
(preinstall) run before install
Pester@4.2.0: Downloading module.
Pester@4.2.0: Importing module.
(install) run after install
(postinstall) run after install
A package.json file:
You can define script in package.json
Please refer run scripts
"scripts": {"hello": "echo 'Hello pspm !'"}
define environment variable for run-scripts.
Please refer run scripts
"config": {"port": "8080"}
To specify the modules your project depends on, you need to list the modules you'd like to use in your package.json file.
"dependencies": {
"<Module name 1>": "<Version>",
"<Module name 2>": "<Version>"
}
You can use npm-semver syntax for specifying versions.
Exact
"1.2.0"
"="
or "v"
is ignored. "v1.2.3" == "1.2.3"
Any
"*"
(asterisk) or ""
(empty) Any versions accepted.Latest
"latest"
Match only newest versionComparators
>=1.0.0
Hyphen Ranges
1.2.3 - 2.3.4 := >=1.2.3 <=2.3.4
X-Ranges
1.x := >=1.0.0 <2.0.0
2.3 := 2.3.x := >=2.3.0 <2.4.0
Caret Ranges
^1.2.3 := >=1.2.3 <2.0.0
^0.0.3 := >=0.0.3 <0.0.4
Tilde Ranges
~1.2.3 := >=1.2.3 <1.3.0
This is valid package.json
sample.
{
"scripts": {
"hello": "echo 'hello pspm'",
"show_port": "echo \"Port: $env:pspm_package_config_port\""
},
"config": {"port": "8080"},
"dependencies": {
"Pester": "4.x",
"AWSPowerShell": "3.3.283.0",
"PSSlack": ">=0.1.0"
}
}
1.8.0
1.7.1
pspm install <user>/<repo-name>#<ref>::path/to/subdir
for fetching subdirectories within github repos.1.6.3
1.6.2
1.6.1
1.6.0
Format-Json
(Thanks @vody!)1.5.1
1.5.0
pspm install @<Repo>/<Name>
for get modules from specific repository.1.4.5
pspm install
command was executed simultaneously in multiple processes.1.4.4
package.json
#721.4.3
1.4.2
GITHUB_TOKEN
is not working properly.1.4.1
1.4.0
GITHUB_TOKEN
is present, pspm uses it as GitHub PAT1.3.0
1.2.3
1.2.1
pspm install
does not function properly in PowerShellGet 1.6+.1.2.0 (deprecated)
1.1.3
1.1.2
1.1.0
pspm run
arguments1.0.0