Skip to content

Commit 8b58c9e

Browse files
author
James Brundage
committed
feat: Mount-Module ( Fixes #1128 )
1 parent 284bc0d commit 8b58c9e

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Commands/Module/Mount-Module.ps.ps1

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[ValidatePattern('Mount[-\.]Module')]
2+
param()
3+
function Mount-Module
4+
{
5+
<#
6+
.SYNOPSIS
7+
Mounts a module.
8+
.DESCRIPTION
9+
Mounts a module as a PSDrive.
10+
11+
This can shorten paths considerably, and make it easier to work with a module's contents.
12+
.EXAMPLE
13+
Get-Module PipeScript | Mount-Module
14+
.EXAMPLE
15+
Mount-Module -Name Microsoft.PowerShell.Management
16+
#>
17+
[Alias('Mount.Module')]
18+
param(
19+
# The name of the module
20+
[vfp()]
21+
[Alias('ModuleName')]
22+
[string]
23+
$Name,
24+
25+
# The root of the module.
26+
[vfp()]
27+
[string]
28+
$Root,
29+
30+
# The description
31+
[vfp()]
32+
[string]
33+
$Description
34+
)
35+
36+
process {
37+
# Get the piped in object
38+
$pipedIn = $_
39+
# If we have no name, return
40+
return if -not $name
41+
42+
if ($pipedIn -is [Management.Automation.PSModuleInfo]) {
43+
if (-not $root) {
44+
$root = $pipedIn | Split-Path
45+
}
46+
}
47+
if (-not $root) {
48+
# Get the root of the module
49+
$root = Get-Module $Name | Select-Object -First 1 | Split-Path
50+
# Return if we could not get the root (no module is loaded)
51+
return if (-not $? -or -not $root)
52+
}
53+
# If we have no description, use the name
54+
if (-not $Description) { $Description = $Name }
55+
# Mount the module
56+
New-PSDrive -Name $name -PSProvider FileSystem -Root $root -Description $description -ErrorAction Ignore -Scope Global
57+
}
58+
}

0 commit comments

Comments
 (0)