-
Notifications
You must be signed in to change notification settings - Fork 19
/
AddPathToPSModulePath.ps1
30 lines (25 loc) · 1.04 KB
/
AddPathToPSModulePath.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<#
.Synopsis
Adds a path to the PSModulePath environment variable.
.DESCRIPTION
Only adds the path if hasn't been added yet. The path will also be added to the $PSModulePath variable so the modules will be available in the current console too.
.EXAMPLE
.\AddPathToPSModulePath.ps1
.\AddPathToPSModulePath.ps1 -Path "C:\MyPowerShellScripts"
#>
Param
(
# The path to a folder that should be added to the list of paths containing PS modules. If not specified, the current path of this script will be added.
[string]
$Path = "$PSScriptRoot\"
)
$paths = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine').Split(';', [System.StringSplitOptions]::RemoveEmptyEntries)
if (-not $paths.Contains($Path))
{
[System.Environment]::SetEnvironmentVariable('PSModulePath', [string]::Join(';', $paths + $Path), 'Machine')
Write-Verbose "The path `"$Path`" was successfully added to the PSModulePath environment variable."
}
else
{
Write-Warning "The PSModulePath environment variable already contains the path `"$Path`"."
}