This repository has been archived by the owner on May 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathMoveFile.ps1
64 lines (58 loc) · 1.7 KB
/
MoveFile.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
<#
.Synopsis
Schedules a file to be moved on reboot.
.DESCRIPTION
Schedules a file to be moved on reboot. This cmdlet can move a file on reboot and optionally
replace an existing file.
.EXAMPLE
Move-FileOnReboot -Path "C:\Windows\System32\kernel32.dll" -Destination "C:\Windows\SysWow64\kernel32.dll" -ReplaceExisting
#>
function Move-FileOnReboot
{
[CmdletBinding()]
param(
# The source file to move.
[Parameter(Mandatory=$true)]
[IO.FileInfo]$Path,
# The destination to move the file to.
[Parameter(Mandatory=$true)]
[IO.FileInfo]$Destination,
# Specifies whether to replace an existing file.
[Parameter()]
[Switch]$ReplaceExisting
)
Begin {
$Flags = [PoshInternals.MoveFileFlags]::MOVEFILE_DELAY_UNTIL_REBOOT
if ($ReplaceExisting)
{
$flags = $flags -bor [PoshInternals.MoveFileFlags]::MOVEFILE_REPLACE_EXISTING
}
if ([PoshInternals.Kernel32]::MoveFileEx($Path, $Destination, $flags) -eq 0)
{
throw New-Object System.Win32Exception
}
}
}
<#
.Synopsis
Schedules a file to be deleted on reboot.
.DESCRIPTION
Schedules a file to be deleted on reboot.
.EXAMPLE
Remove-FileOnReboot -Path "C:\Windows\System32\kernel32.dll"
#>
function Remove-FileOnReboot
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[IO.FileInfo]$Path
)
Begin {
$Flags = [PoshInternals.MoveFileFlags]::MOVEFILE_DELAY_UNTIL_REBOOT
if ([PoshInternals.Kernel32]::MoveFileEx($Path, $null, $Flags) -eq 0)
{
throw New-Object System.Win32Exception
}
}
}