-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMove-Drive.ps1
89 lines (79 loc) · 2.33 KB
/
Move-Drive.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
<#
.Synopsis
Change a drive letter.
.DESCRIPTION
Change a drive letter for a device to another letter.
To run this command locally it must run under elevated privileges. If a computername is specified
your credentials should have enaough permissions to perform the operation.
.EXAMPLE
Move-Drive
This command requires elevated privileges
Run the command as an administrator
.EXAMPLE
move-drive e f tech-01 -verbose
VERBOSE: Moved drive name E to F
#>
function Move-Drive
{
Param(
# Target drive letter
[Parameter(position=1)]
[ValidateLength(1,1)]
[ValidatePattern("[a-z]")]
[string]
$Target = 'F'
, # Drive destination to set
[Parameter(position=2)]
[ValidateLength(1,1)]
[ValidatePattern("[a-z]")]
[ValidateScript({ $psItem -ne $Target })]
[string]
$Destination = 'Z'
,# Target Computer perform the remap
[Parameter(position=3)]
[string]
$ComputerName = 'localhost'
)
Begin{
if( $ComputerName -eq 'localhost' -and -not (
[Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")
)
{
throw "This command requires elevated privileges"
}
$Target = $Target.ToUpper()
$Destination = $Destination.ToUpper()
if($Target -eq $Destination)
{
throw "Target ($Target) matches destination ($Destination). Exiting."
}
$Query = @{
Class = "Win32_Volume"
Filter = "DriveLetter='$Target`:'"
ComputerName = $ComputerName
}
}
Process
{
$drive = Get-WmiObject @Query
if ( $drive )
{
$drive.DriveLetter = "$Destination`:"
} else {
Write-Warning "$Target Drive not found."
return
}
try
{
Write-Progress -Activity "Working..." -Status "Move drive name $Target to $Destination"
$drive.put() > $null
}
catch
{
Write-Error "Cannot move drive $Target`: to $Destination`:"
return
}
Write-Verbose "Moved drive name $Target to $Destination"
}
}