From 7e9743e3439a6c0bf684216b01e477241e0377b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pl=C3=ADnio=20Larrubia?= Date: Tue, 10 Oct 2023 17:11:41 -0300 Subject: [PATCH] Allow WinDebloatTools to run asking for admin perms + + Run Windows Terminal or PowerShell depending on winget. - If script is in the wrong folder, it'll be able to detect and correct it. --- WinDebloatTools.ps1 | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/WinDebloatTools.ps1 b/WinDebloatTools.ps1 index bfedd0ff..a9710a9a 100644 --- a/WinDebloatTools.ps1 +++ b/WinDebloatTools.ps1 @@ -1,4 +1,4 @@ -# Learned from: https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.1 +# Learned from: https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.1 # Adapted majorly from https://github.com/ChrisTitusTech/win10script and https://github.com/Sycnex/Windows10Debloater # Take Ownership tweak from: https://www.howtogeek.com/howto/windows-vista/add-take-ownership-to-explorer-right-click-menu-in-vista/ @@ -41,6 +41,11 @@ function Main() { Import-Module -DisableNameChecking "$PSScriptRoot\src\utils\Individual-Tweaks.psm1" -Force Import-Module -DisableNameChecking "$PSScriptRoot\src\utils\Install-Individual-System-Apps.psm1" -Force + If ("$pwd" -notlike "$PSScriptRoot") { + Write-Host "Wrong location detected, changing to script folder!" -BackgroundColor Yellow + Set-Location -Path "$PSScriptRoot" + } + Set-ConsoleStyle $CurrentFileName = (Split-Path -Path $PSCommandPath -Leaf).Split('.')[0] $CurrentFileLastModified = (Get-Item "$(Split-Path -Path $PSCommandPath -Leaf)").LastWriteTimeUtc | Get-Date -Format "yyyy-MM-dd" @@ -103,7 +108,14 @@ function Open-DebloatScript { function Request-AdminPrivilege() { # Used from https://stackoverflow.com/a/31602095 because it preserves the working directory! - If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } + If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { + Try { + winget --version + Start-Process -Verb RunAs -FilePath "wt.exe" -ArgumentList "--startingDirectory `"$PSScriptRoot`" --profile `"Windows PowerShell`"", "cmd /c powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""; taskkill.exe /f /im $PID; exit + } Catch { + Start-Process -Verb RunAs -FilePath powershell.exe -ArgumentList "-NoProfile", "-ExecutionPolicy Bypass", "-File `"$PSCommandPath`""; exit + } + } } function Show-GUI() {