Skip to content

Instantly share code, notes, and snippets.

@rob89m
Last active November 29, 2021 09:30
Show Gist options
  • Save rob89m/6bbea14651396f5870b23f1b2b8e4d0d to your computer and use it in GitHub Desktop.
Save rob89m/6bbea14651396f5870b23f1b2b8e4d0d to your computer and use it in GitHub Desktop.
Remove-ItemOnReboot #Powershell #Registry #Delete
<#
.DESCRIPTION
Adds defined item to the PendingFileRenameOperations value in Registry to be deleted on next reboot.
.EXAMPLE
If item to be removed is a file
Remove-ItemOnReboot -Item "C:\Temp\image.wim"
If item to be removed is a directory
Remove-ItemOnReboot -Item "C:\Temp\"
.PARAMETER Item
A path to item to be removed on next reboot.
#>
Function Remove-ItemOnReboot
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][string]$Item
)
END
{
# Read current items from PendingFileRenameOperations in Registry
$PendingFileRenameOperations = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations).PendingFileRenameOperations
# Append new item to be deleted to variable
$NewPendingFileRenameOperations = $PendingFileRenameOperations + "\??\$Item"
# Reload PendingFileRenameOperations with existing values plus newly defined item to delete on reboot
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name "PendingFileRenameOperations" -Value $NewPendingFileRenameOperations
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment