Visual Studio 开发人员命令提示和开发人员 PowerShell

Visual Studio 包含两个开发人员命令行 shell,一个命令提示符和一个 PowerShell 实例,如下所示:

  • Visual Studio 开发人员命令提示 - 一种设置了某些环境变量的标准命令提示,可以让使用命令行开发人员工具变得更容易。

    Screenshot of the Developer Command Prompt for Visual Studio 2019 showing clrver tool.

    Screenshot of the Developer Command Prompt for Visual Studio 2022 that shows the clrver tool.

  • Visual Studio 开发人员 PowerShell - 比命令提示符更强大。 例如,你可以将一个命令的输出(称为 cmdlet)传递给另一个 cmdlet。 此 shell 的环境变量集与开发人员命令提示的相同。 自 Visual Studio 2019 起可用。

    Screenshot of the Developer PowerShell tool in Visual Studio 2022.

Visual Studio 2019 开始,Visual Studio 包含一个集成终端,此终端可以托管这些 shell(开发人员命令提示和开发人员 PowerShell)中的任何一个。 你还可以打开每个 shell 的多个选项卡。 Visual Studio 终端是在 Windows 终端的基础上生成的。 若要在 Visual Studio 中打开此终端,请选择“查看”>“终端”。

Screenshot of the Visual Studio terminal pane that shows multiple tabs.

Screenshot of the Visual Studio terminal showing multiple tabs.

提示

本页介绍如何在 Visual Studio 中使用命令行 shell。 如果要在 Visual Studio Code(也称为 VS Code)中查找等效项,请参阅命令行接口 (CLI) 终端基础知识

从 Visual Studio 作为一个单独的应用程序打开其中一个开发人员 shell,或者在终端窗口中打开其中一个开发人员 shell 时,它会打开转到当前解决方案的目录(如果已加载解决方案)。 通过此行为,你可以便捷地针对解决方案或其项目运行命令。

这两个 shell 都具有特定的环境变量集,使你可以更轻松地使用命令行开发人员工具。 打开其中一个 shell 后,可以输入针对不同实用程序的命令,而无需知道它们的位置。

常用命令 说明
MSBuild 生成项目或解决方案
clrver 用于 CLR 的 .NET Framework 工具
ildasm 用于反汇编程序的 .NET Framework 工具
dotnet .NET CLI 命令
dotnet run .NET CLI 命令
CL C/C++ 编译工具
NMAKE C/C++ 编译工具
LIB C/C++ 生成工具
DUMPBIN C/C++ 生成工具

在 Visual Studio 中启动

按照以下步骤从 Visual Studio 内部打开“开发人员命令提示”或“开发人员 PowerShell”:

  1. 打开 Visual Studio。

  2. 在菜单栏上,依次选择“工具”>“命令行”>“开发人员命令提示”或“开发人员 PowerShell”。

    Screenshot of the Command Line menu in Visual Studio 2022.

    Screenshot of the Command Line menu in Visual Studio 2019.

从 Windows 菜单中启动

启动 shell 的另一种方法是从“开始”菜单启动。 你可能会有多个命令提示符,具体取决于你安装的 Visual Studio 的版本及其他任何 SDK 和工作负载。

Windows 11

  1. 选择“开始”Screenshot of the Start button in Windows 11.,然后在“在此处键入以搜索”对话框中输入 developer command promptdeveloper powershell

  2. 选择与搜索文本相关联的应用结果。

Windows 10

  1. 选择“开始”Screenshot of the Start button in Windows 10.,然后滚动到字母 V。

  2. 展开“Visual Studio 2019”或“Visual Studio 2022”文件夹。

  3. 如果运行的是 Visual Studio 2019,请选择“VS 2019 开发人员命令提示”或“VS 2019 开发人员 PowerShell”。 如果运行的是 Visual Studio 2022,请选择“VS 2022 开发人员命令提示”或“VS 2022 开发人员 PowerShell”。

    或者,你可以首先在任务栏的搜索框中键入 shell 的名称,然后在结果列表开始显示搜索匹配项时选择所需的结果。

    An animation that shows the search behavior in Windows 10.

Windows 8.1

  1. 通过按键盘上的 Windows 徽标键 Screenshot of the Windows logo key on the keyboard.(举例说明)转到“开始”屏幕。

  2. 在“开始”屏幕上,按 Ctrl+Tab 打开“应用程序”列表,然后按 V。然后显示一个列表,其中包含所有已安装的 Visual Studio 命令提示。

  3. 如果运行的是 Visual Studio 2019,请选择“VS 2019 开发人员命令提示”或“VS 2019 开发人员 PowerShell”。 如果运行的是 Visual Studio 2022,请选择“VS 2022 开发人员命令提示”或“VS 2022 开发人员 PowerShell”。

Windows 7

  1. 选择“开始”,然后展开“所有程序”。

  2. 依次选择“Visual Studio 2019”>“Visual Studio Tools”>“VS 2019 开发人员命令提示”或“VS 2019 开发人员 PowerShell”。 (如果运行的是 Visual Studio 2022,请查找包含“2022”而不是“2019”的相同项。)

    Screenshot of the Windows 7 Start menu with the command prompt highlighted.

如果安装了其他 SDK,例如 Windows SDK之前的版本,则可能看到其他命令提示符。 查看单个工具的文档,以确定应使用哪个版本的命令提示。

从文件浏览器启动

已安装的 shell 的快捷方式通常放在 Visual Studio 的“开始菜单”文件夹中,例如 %ProgramData%\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools。 但是如果搜索命令提示未产生预期的效果,你可以尝试在计算机中手动查找文件。

开发人员命令提示

搜索命令提示文件的名称,即 VsDevCmd.bat,或者转到 Visual Studio 的“工具”文件夹,例如 %ProgramFiles%\Microsoft Visual Studio\2022\Community\Common7\Tools(该路径根据你的 Visual Studio 版本和安装位置而变化)。

找到命令提示符文件后,在常规的命令提示符窗口中输入以下命令以将其打开:

"%ProgramFiles%\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"

或者在 Windows“运行”对话框中输入以下命令:

%comspec% /k "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"

或者在 Windows“运行”对话框中输入以下命令:

%comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"

提示

请确保编辑路径以匹配所使用的 Visual Studio 版本。

开发人员 PowerShell

搜索名为 Launch-VsDevShell.ps1 的 PowerShell 脚本文件,或转到 Visual Studio 的“工具”文件夹,例如 %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\Common7\Tools。 (该路径根据你的 Visual Studio 版本和安装位置而变化。)找到 PowerShell 文件后,在 Windows PowerShell 或 PowerShell 6 提示符处输入以下命令以运行它。

对于 Visual Studio 2019:

& 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Launch-VsDevShell.ps1'

对于 Visual Studio 2022:

& 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1'

默认情况下,已为 Visual Studio 安装配置了启动的开发人员 PowerShell,而 Launch-VsDevShell.ps1 文件就位于 Visual Studio 的安装路径中。

提示

必须设置执行策略才能运行 cmdlet。

Launch-VsDevShell.ps1 脚本的工作原理是:在 Visual Studio 安装路径中查找 Microsoft.VisualStudio.DevShell.dll PowerShell 模块,加载该模块,然后调用 Enter-VsDevShell cmdlet。 已安装的快捷方式(如“开始”菜单中的快捷方式)加载模块并直接调用 cmdlet。 Launch-VsDevShell.ps1 是以交互方式初始化开发人员 PowerShell 或用于脚本生成自动化的建议方法。

命令行参数

可以对 shell、开发人员命令提示或开发人员 PowerShell 使用命令行参数。

目标体系结构和主机体系结构

对于创建面向特定 CPU 体系结构的输出的生成工具(如 C++ 编译器),开发人员 shell 可以通过相应的命令行参数进行配置。 还可以使用命令行参数来配置生成工具二进制文件的体系结构。 当生成计算机的体系结构不同于目标体系结构时,这非常有用。

提示

从 Visual Studio 2022 开始,无论主机体系结构如何,msbuild 都将默认为 64 位 msbuild.exe 二进制文件。

Shell 参数
开发人员命令提示 -arch=<目标体系结构>
开发人员命令提示 -host_arch=<主机体系结构>
开发人员 PowerShell -Arch <目标体系结构>
开发人员 PowerShell -HostArch <主机体系结构>

重要

开发人员 PowerShell 参数 -Arch 和 -HostArch 仅从 Visual Studio 2022 版本 17.1 开始可用。

下表列出了支持的体系结构,并说明了它们是否可用于目标体系结构参数或主机体系结构参数。

体系结构 目标体系结构 主机体系结构
x86 默认 默认
amd64
arm
arm64

提示

如果仅设置目标体系结构,则 shell 会尝试匹配主机体系结构。 如果只将目标体系结构设置为主机体系结构也不支持的值,这可能会导致错误。

示例

在 64 位计算机上启动 Visual Studio 2019 Community Edition 开发人员命令提示,创建面向 64 位的生成输出:

"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat" -arch=amd64

在 64 位计算机上启动 Visual Studio 2019 Community Edition 开发人员命令提示,创建面向 arm 的生成输出:

"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat" -arch=arm -host_arch=amd64

在 64 位计算机上启动 Visual Studio 2022 版本 17.1 或更高版本的 Community Edition 开发人员 PowerShell,创建面向 arm64 的生成输出:

& 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1' -Arch arm64 -HostArch amd64

SkipAutomaticLocation

对于 Developer PowerShell,shell 的起始目录是 Visual Studio Project Location。 此默认区域设置替代了任何其他路径,例如工作目录。 可以使用命令行参数 -SkipAutomaticLocation 禁用此行为。 如果你要让 shell 在初始化后保留在当前目录中,这非常有用。

可在“工具”>“选项”>“项目和解决方案”>“项目位置”中调整项目位置

提示

Launch-VsDevShell.ps1 脚本和 Enter-VsDevShell cmdlet 都支持命令行参数 -Arch-HostArch-SkipAutomaticLocation