diff --git a/README.md b/README.md index e4a5798..905c726 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ The current 25H2 build of Windows 11 and future builds will include increasingly --- > [!WARNING] - > Running the script with PowerShell 7 can cause issues, to avoid this ensure you are running Windows PowerShell (5.1) + > Running the script with PowerShell 7 is no longer supported and it WILL cause issues, to avoid this ensure you are running Windows PowerShell (5.1) > ### Launch with UI @@ -196,3 +196,4 @@ If you would like to support my work consider donating :) + diff --git a/RemoveWindowsAi.ps1 b/RemoveWindowsAi.ps1 index fbfc407..76a6a5f 100644 --- a/RemoveWindowsAi.ps1 +++ b/RemoveWindowsAi.ps1 @@ -27,6 +27,32 @@ if ($nonInteractive) { } +#get powershell version to ensure run-trusted doesnt enter an infinite loop +$version = $PSVersionTable.PSVersion +if ($version -like '7*') { + $Global:psversion = 7 +} +else { + $Global:psversion = 5 +} + +if ($psversion -ge 7) { + Write-Host "ERROR: This script requires Windows PowerShell 5.1 (powershell.exe)." -ForegroundColor Red + Write-Host "You are currently running PowerShell version $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)." -ForegroundColor Red + Write-Host "PowerShell 7+ (pwsh.exe) is not supported. Please run the script using the classic Windows PowerShell 5.1." -ForegroundColor Red + if (-not $nonInteractive) { + try { + Add-Type -AssemblyName System.Windows.Forms + [System.Windows.Forms.MessageBox]::Show( + "This script must be run in Windows PowerShell 5.1.`n`nCurrent version: $($PSVersionTable.PSVersion)`n`nPlease use powershell.exe instead of pwsh.exe.", + "PowerShell Version Error", + [System.Windows.Forms.MessageBoxButtons]::OK, + [System.Windows.Forms.MessageBoxIcon]::Error + ) | Out-Null + } catch { } + } + exit 1 +} If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator')) { #leave out the trailing " to add supplied params first @@ -189,12 +215,7 @@ function Run-Trusted([String]$command, $psversion) { # changed start-process to wshell run to avoid the first powershell instance window from flashing - if ($psversion -eq 7) { - $psexe = 'pwsh.exe' - } - else { - $psexe = 'PowerShell.exe' - } + $psexe = 'PowerShell.exe' #convert command to base64 to avoid errors with spaces $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) @@ -264,15 +285,6 @@ function Write-Status { Write-Host '~ ~ ~ Remove Windows AI by @zoicware ~ ~ ~' -ForegroundColor DarkCyan -#get powershell version to ensure run-trusted doesnt enter an infinite loop -$version = $PSVersionTable.PSVersion -if ($version -like '7*') { - $Global:psversion = 7 -} -else { - $Global:psversion = 5 -} - if ($EnableLogging) { $date = (Get-Date).ToString('MM-dd-yyyy-HH:mm') -replace ':' $Global:logPath = "$env:USERPROFILE\RemoveWindowsAI$date.log" @@ -3597,4 +3609,5 @@ if (!$nonInteractive) { $Host.UI.RawUI.ReadKey() *>$null } -exit \ No newline at end of file +exit +