将 .ps1 的默认"编辑"操作从 ISE 更改为 VS 代码



就像这个问题中的人一样,如此明显的事情却没有明确的答案,这令人沮丧(我看了20页,什么都没有发现(。

默认的右键单击";编辑";在Windows中,.ps1文件的操作是使用PowerShell_ISE打开。ISE是一个古老而臃肿的旧时代遗迹,打开速度非常慢,所以当你不小心右键点击>编辑,看着ISE慢慢打开是曲折的。

我们如何以编程方式更改默认的";编辑";.ps1/.psm1等的操作,以便它们指向VS代码而不是ISE?

或者,如果我们不能更改它,我们可以完全删除它吗(这样"用VS代码编辑"是唯一剩下的选项(?

这实际上在PowerShell中很容易做到,或者手动调整注册表。

这是一个可以改变现状的剧本。

因为要更改HKEY_CLASSES_ROOT,所以需要以管理员身份运行:

# This a "provider qualified" path to the edit command
$editRegistryPath = 'Microsoft.PowerShell.CoreRegistry::HKEY_CLASSES_ROOTSystemFileAssociations.ps1ShellEditCommand'
# This is how we will get the default value
$defaultEdit = Get-ItemProperty $editRegistryPath  -Name '(default)'
# If we couldn't get it, something's wrong, so return
if (-not $defaultEdit) {
return
}
# Get the path to code.cmd (this must be in $env:PATH)
$codePath = Get-Command code -CommandType Application | Select-Object -First 1 | Select-Object -ExpandProperty Source
# Change the property.  Make sure we quote our command name and our arguments.
Set-ItemProperty -Path $editRegistryPath -Name '(default)' -Value "`"$($codePath)`" `"%1`""

您只需在以下注册表项中将exe路径更改为vs代码

ComputerHKEY_CLASSES_ROOTSystemFileAssociations.ps1ShellEditCommand

双击默认属性并将ISE路径替换为vscode.exe 的路径

最新更新