Ask about running with admin rights also when "run with powershell" #389

Windows10Debloater really needs to run with administrative rights,
otherwise it cannot ultimately remove the apps.

The problem was that the prompt to run as administrator
only worked within the ISE. It did not work if you
right-click on the ps1 and "run with PowerShell"
on a new installation of Windows.

This second case is by far the only way I would run a ps1,
and so it was always silently starting as a regular user,
and thus was largely ineffecive in actually removing bloat.

The host.ui displays a GUI prompt if available,
or a text prompt if not.
It also nicely allows setting a default value,
which I set to run as administrator.
This commit is contained in:
Justin Luth 2021-10-28 13:42:32 +02:00
parent a48b4d8dc5
commit 9086e562ec

View File

@ -10,8 +10,12 @@ $EnableEdgePDFTakeover.Location = New-Object System.Drawing.Point(155, 260)
$ErrorActionPreference = 'SilentlyContinue' $ErrorActionPreference = 'SilentlyContinue'
$Button = [System.Windows.MessageBoxButton]::YesNoCancel ## The following four lines only need to be declared once in your script.
$ErrorIco = [System.Windows.MessageBoxImage]::Error $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($no, $yes, $cancel)
$Ask = 'Do you want to run this as an Administrator? $Ask = 'Do you want to run this as an Administrator?
Select "Yes" to Run as an Administrator Select "Yes" to Run as an Administrator
@ -20,16 +24,17 @@ $Ask = 'Do you want to run this as an Administrator?
Select "Cancel" to stop the script.' Select "Cancel" to stop the script.'
If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator')) { If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator')) {
$Prompt = [System.Windows.MessageBox]::Show($Ask, "Run as an Administrator or not?", $Button, $ErrorIco) $Prompt = $host.ui.PromptForChoice($Ask, "Run as an Administrator or not?", $options, 1)
Switch ($Prompt) { Switch ($Prompt) {
#This will debloat Windows 10 #This will debloat Windows 10
Yes { 1 { #Yes
Write-Host "You didn't run this script as an Administrator. This script will self elevate to run as an Administrator and continue." Write-Host "You didn't run this script as an Administrator. This script will self elevate to run as an Administrator and continue."
Start-Process PowerShell.exe -ArgumentList ("-NoProfile -ExecutionPolicy Bypass -File `"{0}`"" -f $PSCommandPath) -Verb RunAs Start-Process PowerShell.exe -ArgumentList ("-NoProfile -ExecutionPolicy Bypass -File `"{0}`"" -f $PSCommandPath) -Verb RunAs
Exit Exit
} }
No { default {
Break Break
} }
} }