Skip to content

Instantly share code, notes, and snippets.

@Damperen
Created February 15, 2022 08:11
Show Gist options
  • Save Damperen/67ed22f34f8fc5ba4d68042260b92e8a to your computer and use it in GitHub Desktop.
Save Damperen/67ed22f34f8fc5ba4d68042260b92e8a to your computer and use it in GitHub Desktop.
This script creates an "Uber Delete" button in the context menu. It will delete pesky files that does not want to be deleted.
#Advanced check if user is administrator. It elevates the powershell to one with administrator privileges if not admin
# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running as an administrator
if ($myWindowsPrincipal.IsInRole($adminRole)){
# We are running as an administrator, so change the title and background colour to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Administrator)"
#$Host.UI.RawUI.BackgroundColor = "DarkBlue"
Clear-Host
}
else {
# We are not running as an administrator, so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell"
# Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
$newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"
# Indicate that the process should be elevated
$newProcess.Verb = "runas"
# Start the new process
[System.Diagnostics.Process]::Start($newProcess)
# Exit from the current, unelevated, process
Exit
}
$command = @'
cmd.exe /s /c pushd "%V" && powershell -command $folderDelete = Get-Location;Write-Host $folderDelete; Read-Host -Prompt "Slet?"; New-Item -Path $folderDelete -Name destroyerofworlds -ItemType Directory; robocopy destroyerofworlds $folderDelete /mir;Set-Location ..;$comm = ("'Remove-Item '" + $folderDelete + "' -Force'");Clear-Host; Sleep -Seconds 2; Start-Process powershell.exe -ArgumentList("'-command'", $comm)
'@
$path = "HKLM:\SOFTWARE\Classes\Directory\shell\UberDelete"
if(-NOT (Test-Path -Path $path)) {
New-Item -Path $path -Name 'command' -Force |
Set-ItemProperty -Name "(Default)" -Value $command
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment