[CmdletBinding()] param() Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $root = $PSScriptRoot $binPath = Join-Path $root 'bin' function Normalize-PathEntry { param([string]$Value) if ([string]::IsNullOrWhiteSpace($Value)) { return '' } return $Value.Trim().TrimEnd('\') } function Remove-UserPath { param([string]$PathToRemove) $current = [Environment]::GetEnvironmentVariable('Path', 'User') $normalizedTarget = Normalize-PathEntry $PathToRemove $entries = @( $current -split ';' | ForEach-Object { Normalize-PathEntry $_ } | Where-Object { $_ -and -not $_.Equals($normalizedTarget, [StringComparison]::OrdinalIgnoreCase) } ) [Environment]::SetEnvironmentVariable('Path', ($entries -join ';'), 'User') Write-Host "Removed from user PATH: $PathToRemove" } function Remove-ProfileBlock { param([string]$ProfilePath) if (-not (Test-Path -LiteralPath $ProfilePath)) { return } $beginMarker = '# >>> MS.Open >>>' $endMarker = '# <<< MS.Open <<<' $content = Get-Content -LiteralPath $ProfilePath -Raw $pattern = "(?ms)^$([regex]::Escape($beginMarker)).*?^$([regex]::Escape($endMarker))\s*" $cleaned = [regex]::Replace($content, $pattern, '').TrimEnd() if ($cleaned) { Set-Content -LiteralPath $ProfilePath -Value "$cleaned`r`n" -Encoding UTF8 } else { Set-Content -LiteralPath $ProfilePath -Value '' -Encoding UTF8 } Write-Host "Removed MS.Open from profile: $ProfilePath" } Remove-UserPath -PathToRemove $binPath $documents = [Environment]::GetFolderPath('MyDocuments') $profiles = @( (Join-Path $documents 'WindowsPowerShell\Microsoft.PowerShell_profile.ps1'), (Join-Path $documents 'PowerShell\Microsoft.PowerShell_profile.ps1') ) | Select-Object -Unique foreach ($profilePath in $profiles) { Remove-ProfileBlock -ProfilePath $profilePath } Write-Host '' Write-Host 'MS.Open has been uninstalled from PATH and PowerShell profiles.' Write-Host 'Restart your terminal. You can then delete this folder.'