如何将Developer PowerShell重新指向Visual Studio 2022的devenv.exe
问题重现步骤
- 卸载Visual Studio 2019
- 安装Visual Studio 2022
- 打开Visual Studio 2022
- 右键点击任意项目
- 在菜单中选择「Open in Terminal」
此时Developer PowerShell启动后会抛出如下错误:
vs : The term 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At C:\Users\jdoe\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:340 char:2
vs "C:\EDW\My Project\My Project.sl ...~~
- CategoryInfo : ObjectNotFound: (C:\Program File...\IDE\devenv.exe:String) [], CommandNotFoundException
- FullyQualifiedErrorId : CommandNotFoundException
** Visual Studio 2022 Developer PowerShell v17.4.4
** Copyright (c) 2022 Microsoft Corporation
解决方案
报错根源是你的PowerShell配置文件(Microsoft.PowerShell_profile.ps1)里的vs别名/函数指向了已卸载的VS2019的devenv.exe路径,VS2022终端启动时会加载这个配置文件,导致路径找不到。
按以下步骤修复:
- 打开PowerShell,输入命令打开配置文件:
notepad $PROFILE - 在文件中定位到第340行附近的
vs相关代码(比如Set-Alias vs "旧的VS2019路径"这类内容) - 将路径修改为VS2022的
devenv.exe实际路径,示例:
注意:根据你安装的VS2022版本(社区/专业/企业)和自定义安装位置调整路径。# 企业版示例 Set-Alias vs "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\devenv.exe" # 社区版示例 Set-Alias vs "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\devenv.exe" - 保存配置文件,关闭并重新启动Developer PowerShell,报错即可解决。
如果找不到vs的定义,直接删除相关行也可以,VS2022的终端环境会自动配置正确的devenv路径。
内容的提问来源于stack exchange,提问作者Summer

