-
Notifications
You must be signed in to change notification settings - Fork 8
/
Install Module.ps1
48 lines (35 loc) · 1.97 KB
/
Install Module.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<#
.Author
Trevor Sullivan
.Date
January 30th, 2012
.Purpose
The purpose of this script is to import a PowerShell module from wherever it
resides on the filesystem. To use this script, simply copy it to the root of
your module's folder, and execute it. Your working directory doesn't matter.
Using this script avoids the need to copy your module to a valid path in the
$env:PSModulePath Windows environment variable. That way, if your module is
part of a source control repository, which may reside outside of the default
paths defined in $env:PSModulePath, you can easily load the module without
the need to copy your module files to your default modules folder.
Note: This script requires that the module folder name match the module name
#>
# Get the path the script is executing from
$ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Path -Parent;
Write-Debug -Message ("Install module script is running from: {0}" -f $ScriptPath);
# Get just the 'leaf' folder name of the script's execution path
$ModuleName = Split-Path -Path $ScriptPath -Leaf;
# Get the parent of the script path, which will be the newly added module search path
$ModulePath = Split-Path -Path $ScriptPath -Parent;
Write-Debug -Message ('$env:PSModulePath is set to: {0}' -f $env:PSModulePath);
# If $env:PSModulePath does not contain the new module path, then add it
if ($env:PSModulePath.Split(';') -notcontains $ModulePath) {
$env:PSModulePath = $env:PSModulePath + (';{0}' -f $ModulePath);
}
# Silently remove the module, in case it is loaded
Remove-Module -Name $ModuleName -ErrorAction SilentlyContinue;
# Load the module
$Module = Import-Module -Name $ModuleName -PassThru;
# Clean up
Remove-Variable -Name Module, ModulePath, ModuleName, ScriptPath;
$Module = $ModulePath = $ModuleName = $ScriptPath = $null;