I've turned Chocolatey's uninstall script in to a one-line uninstall, which runs perfectly in Powershell, but doesn't work if I add it into a batch script.
Pasting this in Powershell Works
$VerbosePreference = 'Continue';if (-not $env:ChocolateyInstall) {$message=@("Chocolatey is not detected as installed. Nothing to do.") -join "`n";Write-Warning $message;return};if (-not (Test-Path $env:ChocolateyInstall)) {$message = @("No Chocolatey installation detected at '$env:ChocolateyInstall'. Nothing to do.") -join "`n";Write-Warning $message;return};$userKey=[Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment');$userPath=$userKey.GetValue('PATH', [string]::Empty, 'DoNotExpandEnvironmentNames').ToString();$machineKey=[Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\ControlSet001\Control\Session Manager\Environment\');$machinePath=$machineKey.GetValue('PATH', [string]::Empty, 'DoNotExpandEnvironmentNames').ToString();$backupPATHs=@("User PATH: $userPath","Machine PATH: $machinePath");$backupFile = "C:\PATH_backups_ChocolateyUninstall.txt";$backupPATHs | Set-Content -Path $backupFile -Encoding UTF8 -Force;$warningMessage = "This could cause issues after reboot where nothing is found if something goes wrong.In that case, look at the backup file for the original PATH values in '$backupFile'.";if ($userPath -like "*$env:ChocolateyInstall*") {Write-Verbose "Chocolatey Install location found in User Path. Removing...";Write-Warning $warningMessage;$newUserPATH = @($userPath -split [System.IO.Path]::PathSeparator | Where-Object { $_ -and $_ -ne "$env:ChocolateyInstall\bin" }) -join [System.IO.Path]::PathSeparator;$userKey.SetValue('PATH', $newUserPATH, 'ExpandString')};if ($machinePath -like "*$env:ChocolateyInstall*") {Write-Verbose "Chocolatey Install location found in Machine Path. Removing...";Write-Warning $warningMessage;$newMachinePATH = @($machinePath -split [System.IO.Path]::PathSeparator | Where-Object { $_ -and $_ -ne "$env:ChocolateyInstall\bin" }) -join [System.IO.Path]::PathSeparator;$machineKey.SetValue('PATH', $newMachinePATH, 'ExpandString')};$agentService = Get-Service -Name chocolatey-agent -ErrorAction SilentlyContinue;if ($agentService -and $agentService.Status -eq 'Running') {$agentService.Stop()};Remove-Item -Path $env:ChocolateyInstall -Recurse -Force;'ChocolateyInstall', 'ChocolateyLastPathUpdate' | ForEach-Object {foreach ($scope in 'User', 'Machine') {[Environment]::SetEnvironmentVariable($_, [string]::Empty, $scope)}};$machineKey.Close();$userKey.Close();if ($env:ChocolateyToolsLocation -and (Test-Path $env:ChocolateyToolsLocation)) {Remove-Item -Path $env:ChocolateyToolsLocation -Recurse -Force};foreach ($scope in 'User', 'Machine') {[Environment]::SetEnvironmentVariable('ChocolateyToolsLocation', [string]::Empty, $scope)};pause
However if I add it in a batch script starting with
start powershell.exe -Command "Powershell script I posted above" it doesn't work.