Skip to content

Instantly share code, notes, and snippets.

@rafa-br34
Last active June 23, 2024 12:06
Show Gist options
  • Save rafa-br34/fb8005fc02ec9dfce38088b43766a3d5 to your computer and use it in GitHub Desktop.
Save rafa-br34/fb8005fc02ec9dfce38088b43766a3d5 to your computer and use it in GitHub Desktop.
Removes useless files from System32
$CurrentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
if (!(New-Object Security.Principal.WindowsPrincipal($CurrentUser)).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Asking for privileges..."
Start-Process -FilePath "powershell" -ArgumentList ('-File', $MyInvocation.MyCommand.Source, $args | %{ $_ }) -Verb RunAs
exit
}
function KillProcess($Name) {
$Process = Get-Process $Name -ErrorAction SilentlyContinue
if ($Process) {
Write-Host "Killing $Name"
$Process | Stop-Process
}
}
Write-Host "Current user is: $($CurrentUser.Name)"
Write-Host "`nDeleting useless executables"
foreach ($File in "wsqmcons.exe", "devicecensus.exe", "CompatTelRunner.exe") {
$Path = "C:\Windows\System32\$File"
$Dest = "C:\Backup_$File"
if (Test-Path $Path -PathType Leaf) {
KillProcess($File)
$ACL = Get-Acl -Path $Path
Write-Host "Changing ACL owner from $($ACL.Owner) to $($CurrentUser.Name)"
$ACL.SetAccessRuleProtection($True, $False)
$ACL.AddAccessRule($(New-Object System.Security.AccessControl.FileSystemAccessRule($CurrentUser.Name, "FullControl", "Allow")))
$ACL.SetOwner($(New-Object System.Security.Principal.Ntaccount($CurrentUser.Name)))
$ACL | Set-Acl -Path $Path
Write-Host "Moving file at $Path to $Dest"
Move-Item -Path $Path -Destination $Dest -Force
}
else {
Write-Host "File $Path not found"
}
}
Write-Host "`nChecking for useless folders"
foreach ($Folder in "%ProgramFiles%\RUXIM") {
if (Test-Path $Folder -PathType Container) {
Write-Host "Useless folder: $Folder"
}
}
Read-Host -Prompt "Press enter to continue"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment