forked from jdhitsolutions/ISEScriptingGeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-PSDriveHere.ps1
143 lines (112 loc) · 4.64 KB
/
New-PSDriveHere.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#requires -version 2.0
Function New-PSDriveHere {
<#
.Synopsis
Create a new PSDrive at the current location.
.Description
This function will create a new PSDrive at the specified location. The default is the current location, but you can specify any PSPath. The function will take the last word of the path and use it as the name of the new PSDrive. If you prefer to use the first word of the location, use -First. If you prefer to specify a totally different name, then use the -Name parameter.
.Parameter Path
The path for the new PSDrive. The default is the current location.
.Parameter Name
The name for the new PSDrive. The default is the last word in the specified location,
unless you use -First.
.Parameter First
Use the first word of the current location for the new PSDrive.
.Parameter SetLocation
Set location to this new drive.
.Example
PS C:\users\jeff\Documents\Enterprise Mgmt Webinar> new-psdrivehere
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Webinar 146.57 FileSystem C:\users\jeff\Documents\Enter...
.Example
PS C:\users\jeff\Documents\Enterprise Mgmt Webinar> new-psdrivehere -first
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Enterprise 146.57 FileSystem C:\users\jeff\Documents\Enter...
.Example
PS C:\> new-psdrivehere HKLM:\software\microsoft
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
microsoft Registry HKEY_LOCAL_MACHINE\software\micr...
.Example
PS C:\> new-psdrivehere -Path "\\jdh-nvnas\files\powershell" -Name PSFiles
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
PSFiles FileSystem \\jdh-nvnas\files\powershell
.Example
PS C:\Users\Jeff\Documents\DeepDive> new-psdrivehere . DeepDive -setlocation
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
DeepDive 130.53 FileSystem C:\Users\Jeff\Documents\DeepDive
PS DeepDive:\>
Set a new PSDrive and change to it.
.Inputs
None. You cannot pipe to this function.
.Outputs
System.Management.Automation.PSDrive
.Link
http://jdhitsolutions.com/blog/2010/08/new-psdrivehere/
.Link
Get-PSDrive
New-PSDrive
.Notes
NAME: New-PSDriveHere
VERSION: 2.0
AUTHOR: Jeffery Hicks
LASTEDIT: Octber 15, 2011
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
#>
[cmdletBinding(SupportsShouldProcess=$True)]
Param(
[Parameter(Position=0)]
[ValidateScript({Test-Path $_})]
[string]$Path=".",
[Parameter(Position=1)]
[string]$Name,
[switch]$First,
[Alias("cd")]
[switch]$SetLocation
)
#get the specified location
$location=Get-Item -Path $path
#did the user specify a name?
if ($Name) {
Write-Verbose "Name parameter used for $name"
} #if $name
else {
if ($first) {
$pattern="^\w+"
}
else {
$pattern="\w+$"
}
#Make sure name contains valid characters. This function
#should work for all but the oddest named folders.
if ($location.Name -match $pattern) {
$name=$matches[0]
}
else {
#The location has something odd about it so bail out
Write-Warning "$path doesn't meet the criteria"
Break
}
} #else $name not specified
#verify a PSDrive doesn't already exist
Write-Verbose "Testing $($name):"
If (-not (Test-Path -path "$($name):")) {
Write-Verbose "Creating PSDrive for $name"
New-PSDrive -Name $name -PSProvider $location.PSProvider -Root $Path `
-Description "Created $(get-date)" -scope Global
if ($SetLocation) {
Write-Verbose "Setting location to $($name):"
set-location -Path "$($name):"
}
}
else {
Write-Warning "A PSDrive for $name already exists"
}
} #function
#create an alias for the function
Set-Alias -Name npsd -Value New-PSDriveHere