liveBook · Manning

PSModulePath environment variable

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

PSModulePath is the PowerShell environment variable containing folders in which PowerShell searches for modules.

PSModulePath is the environment variable that lists folders PowerShell searches for modules. A module placed in one of those folders is recognized automatically and can be imported by name, making the path central to reusable module deployment. section 12.2.3

For a custom module, installing under PSModulePath makes the module easier for both its creator and other users to find and use. Pair that placement with a manifest that lists dependencies, clear Verb-Noun command names, and a distinction between public commands and helper functions. section 2.2.3 section 12.2.3

A team installer can make a repository-backed module available without requiring the repository itself to live in a module-path folder:

  • Check whether Git is installed; install it through a package manager appropriate to the operating system when needed. section 12.2.3
  • Clone the remote repository to a local location. section 12.2.3
  • Create a symbolic link from a folder in PSModulePath to the repository folder, so PowerShell recognizes the module while the repository remains elsewhere. section 12.2.3

Shared repository module

A team can clone a shared module repository anywhere on each machine, then link it beneath a PSModulePath folder. The link avoids operating-system differences in module-path locations and avoids relying on profiles that vary by PowerShell version and console. section 12.2.3

Listing 12.5 Install-PoshAutomator.ps1
$RepoUrl =                                                  #1
    'https://github.com/<yourprofile>/PoshAutomator.git'
Function Test-CmdInstall {
    param(
        $TestCommand
    )
    try {
        $Before = $ErrorActionPreference
        $ErrorActionPreference = 'Stop'
        $testResult = Invoke-Expression -Command $TestCommand
    }
    catch {
        $testResult = $null
    }
    finally {
        $ErrorActionPreference = $Before
    }
    $testResult
}
 
Function Set-EnvPath{
    $env:Path =                                             #2
        [System.Environment]::GetEnvironmentVariable("Path", "Machine") +
        ";" +
        [System.Environment]::GetEnvironmentVariable("Path", "User")
}
$GitVersion = Test-CmdInstall 'git --version'               #3
if (-not ($GitVersion)) {
    if($IsWindows){
        Write-Host "Installing Git for Windows..."
        $wingetParams = 'winget install --id Git.Git' +
            ' -e --source winget --accept-package-agreements' +
            ' --accept-source-agreements'
        Invoke-Expression  $wingetParams
    }
    elseif ($IsLinux) {
        Write-Host "Installing Git for Linux..."
        apt-get install git -y
    }
    elseif ($IsMacOS) {
        Write-Host "Installing Git for macOS..."
        brew install git -y
    }
    Set-EnvPath                                             #4
    $GitVersion = Test-CmdInstall 'git --version'
    if (-not ($GitVersion)) {
        throw "Unable to locate Git.exe install.
            Please install manually and rerun this script."
    }
    else{
        Write-Host "Git Version $($GitVersion) has been installed"
    }
}
else {
    Write-Host "Git Version $($GitVersion) is already installed"
}
 
if($IsWindows){                                             #5
    Set-Location $env:USERPROFILE
}
else {
    Set-Location $env:HOME
}
 
Invoke-Expression -Command "git clone $RepoUrl"             #6
 
$ModuleFolder = Get-Item './PoshAutomator'
$UserPowerShellModules =                                    #7
    [Environment]::GetEnvironmentVariable("PSModulePath").Split(';')[0]
$SimLinkProperties = @{                                     #8
    ItemType = 'SymbolicLink'
    Path     = (Join-Path $UserPowerShellModules $ModuleFolder.BaseName)
    Target   = $ModuleFolder.FullName
    Force    = $true
}
New-Item @SimLinkProperties

The installer listing combines Git detection, operating-system-specific installation, repository cloning, and environment-path setup into a reusable deployment script. section 12.2.3

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains module discovery and a symbolic-link installation workflow, while deferring platform-specific package-manager choices to the environment.
Examples
Demonstrates a Git-cloned shared repository linked into a PowerShell module-path folder by an installation script.
What it assumes
Assumes a supported package manager and uses Git availability as an installation prerequisite.
What you can do after
Operate/configure shared module installation by linking a cloned repository into a PSModulePath folder.
How it connects
Connects module discovery with module manifests, dependencies, Import-Module, symbolic links, Git, and PowerShell profiles.

Also explained in

Compare how 2 books explain PSModulePath environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.