如何通过脚本临时取消Windows Terminal默认终端应用设置?
解决方案
Windows 11的默认终端设置存储在注册表中,而非Windows Terminal的settings.json。你可以通过PowerShell脚本临时切换默认终端为Windows Console Host,运行目标ps1后再恢复原设置,具体步骤如下:
1. 编写包装脚本
创建一个名为RunPs1WithoutWT.ps1的脚本,内容如下:
param( [Parameter(Mandatory=$true)] [string]$Ps1Path ) # 备份当前默认终端及委派设置 $currentDefaultTerminal = Get-ItemPropertyValue -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Terminal\DefaultTerminal" -Name "DefaultTerminal" -ErrorAction SilentlyContinue $delegationConsole = Get-ItemPropertyValue -Path "HKCU:\Console\%%Startup" -Name "DelegationConsole" -ErrorAction SilentlyContinue $delegationTerminal = Get-ItemPropertyValue -Path "HKCU:\Console\%%Startup" -Name "DelegationTerminal" -ErrorAction SilentlyContinue # 切换默认终端为Windows Console Host Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Terminal\DefaultTerminal" -Name "DefaultTerminal" -Value "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}" -Type String -Force # 禁用控制台委派,确保PowerShell直接使用Console Host if (-not (Test-Path "HKCU:\Console\%%Startup")) { New-Item -Path "HKCU:\Console\%%Startup" -Force | Out-Null } Set-ItemProperty -Path "HKCU:\Console\%%Startup" -Name "DelegationConsole" -Value 0 -Type DWord -Force Set-ItemProperty -Path "HKCU:\Console\%%Startup" -Name "DelegationTerminal" -Value 0 -Type DWord -Force # 运行目标ps1脚本,等待执行完成 Start-Process powershell.exe -ArgumentList "-File `"$Ps1Path`"" -Wait # 恢复原默认终端设置 if ($currentDefaultTerminal) { Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Terminal\DefaultTerminal" -Name "DefaultTerminal" -Value $currentDefaultTerminal -Type String -Force } else { Remove-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Terminal\DefaultTerminal" -Name "DefaultTerminal" -Force -ErrorAction SilentlyContinue } # 恢复原控制台委派设置 if ($delegationConsole -ne $null) { Set-ItemProperty -Path "HKCU:\Console\%%Startup" -Name "DelegationConsole" -Value $delegationConsole -Type DWord -Force } else { Remove-ItemProperty -Path "HKCU:\Console\%%Startup" -Name "DelegationConsole" -Force -ErrorAction SilentlyContinue } if ($delegationTerminal -ne $null) { Set-ItemProperty -Path "HKCU:\Console\%%Startup" -Name "DelegationTerminal" -Value $delegationTerminal -Type DWord -Force } else { Remove-ItemProperty -Path "HKCU:\Console\%%Startup" -Name "DelegationTerminal" -Force -ErrorAction SilentlyContinue }
2. 使用方法
- 直接在PowerShell中调用脚本,传入目标ps1路径:
.\RunPs1WithoutWT.ps1 -Ps1Path "C:\Scripts\YourScript.ps1" - 若要实现右键直接调用,可添加右键菜单项:
- 打开注册表编辑器,定位到
HKCR:\Microsoft.PowerShellScript.1\Shell - 新建名为
RunWithoutWT的项,设置默认值为“用PowerShell打开(不经过WT)” - 在
RunWithoutWT下新建Command项,设置默认值为:powershell.exe -File "C:\Path\To\RunPs1WithoutWT.ps1" -Ps1Path "%1"
- 打开注册表编辑器,定位到
关键说明
- 注册表路径
HKCU:\Software\Microsoft\Windows\CurrentVersion\Terminal\DefaultTerminal存储默认终端的GUID:- Windows Terminal的GUID:
{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b} - Windows Console Host的GUID:
{0caa0dad-35be-5f56-a8ff-afceeeaa6101}
- Windows Terminal的GUID:
- 修改的是当前用户(HKCU)的注册表,无需管理员权限
- 脚本会自动备份并恢复原设置,不会影响日常默认终端的使用
内容的提问来源于stack exchange,提问作者Oprex Droid
相关产品推荐
相关产品推荐

