Visual Studio 開發人員命令提示字元和開發人員 PowerShell

Visual Studio 包含兩個適用於開發人員的命令列殼層、命令提示字元和 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。 此殼層與開發人員命令提示字元有相同的環境變數集。 自 Visual Studio 2019 起提供。

    Screenshot of the Developer PowerShell tool in Visual Studio 2022.

從 Visual Studio 2019 開始,Visual Studio 包含整合式終端機,可裝載這些殼層中的任一個 (開發人員命令提示字元和開發人員 PowerShell)。 您也可以開啟每個殼層的多個索引標籤。 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 中使用命令列殼層。 如果您要在 Visual Studio Code 中尋找對等項目 (也稱為 VS Code),請參閱命令列介面 (CLI)終端機基本概念

當您從 Visual Studio 開啟其中一個開發人員殼層 (可以個別應用程式或在終端機視窗中) 時,其會開啟至您目前解決方案的目錄 (如果您已載入解決方案)。 此行為可方便針對解決方案或其專案執行命令。

這兩個殼層都有特定的環境變數集,可讓您更輕鬆地使用命令列開發人員工具。 開啟其中一個殼層之後,您可以輸入不同公用程式的命令,而不需要知道其所在位置。

常用命令 描述
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 功能表開始

另一種啟動殼層的方式是從 [開始] 功能表。 根據您已安裝的 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 2019Visual Studio 2022 資料夾。

  3. 如果您執行 Visual Studio 2019,請選取 VS 2019 的開發人員命令提示字元VS 2019 的開發人員 PowerShell。 如果您執行 Visual Studio 2022,請選取 VS 2022 的開發人員命令提示字元VS 2022 的開發人員 PowerShell

    或者,您可以在工作列的搜尋方塊中開始輸入殼層的名稱,然後在結果清單開始顯示搜尋相符項目時,選取您想要的結果。

    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.

如果已安裝 Windows SDK舊版之類的其他 SDK,您可能會看到其他命令提示字元。 查看個別工具的文件以判斷要使用哪個版本的命令提示字元。

從檔案瀏覽器開始

您已安裝殼層的捷徑通常會位於 Visual Studio 的 [開始功能表] 資料夾中,例如 %ProgramData%\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools 中。 但如果搜尋命令提示字元未產生預期的結果,您可以嘗試以手動方式尋找電腦上的檔案。

開發人員命令提示字元

搜尋 VsDevCmd.bat 等命令提示字元檔案名稱,或移至 %ProgramFiles%\Microsoft Visual Studio\2022\Community\Common7\Tools 之類的 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'

根據預設,針對 Launch-VsDevShell.ps1 檔案所在安裝路徑的 Visual Studio 安裝,會設定啟動的開發人員 PowerShell。

提示

必須設定執行原則,才能執行 Cmdlet。

Launch-VsDevShell.ps1 指令碼的運作方式是尋找 Microsoft.VisualStudio.DevShell.dll Visual Studio 安裝路徑中的 PowerShell 模組、加以載入,然後叫用 Enter-VsDevShell Cmdlet。 已安裝的捷徑 (如同 [開始] 功能表中的捷徑一樣) 會載入模組並直接叫用 Cmdlet。 Launch-VsDevShell.ps1 是以互動方式初始化開發人員 PowerShell 或指令碼建置自動化的建議方式。

命令列引數

您可以針對殼層、開發人員命令提示字元或開發人員 PowerShell 使用命令列引數。

目標架構和主機架構

建置工具 (例如 C++ 編譯器) 會建立以特定 CPU 架構為目標的輸出,可以使用適當的命令列引數來設定開發人員殼層。 您也可以使用命令列引數來設定建置工具二進位檔的架構。 當組建電腦的架構與目標架構不同時,這會非常有用。

提示

從 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 Yes Yes
arm No
arm64 No

提示

如果您只設定目標架構,則殼層會嘗試使主機架構相符。 當只有目標架構設定為主機架構不支援的值時,這才可能會導致錯誤。

範例

在 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

針對開發人員 PowerShell,殼層的起始目錄是 Visual Studio 專案位置。 此預設地區設定會覆寫任何其他路徑,例如工作目錄。 您可以使用命令列引數 -SkipAutomaticLocation 來關閉此行為。 如果您想要殼層在初始化之後留在目前的目錄中,這會非常有用。

可以在 [工具]>[選項]>[專案和解決方案]>[專案位置] 中調整專案位置。

提示

Launch-VsDevShell.ps1 指令碼和 Enter-VsDevShell Cmdlet 都支援命令列引數 -Arch-HostArch-SkipAutomaticLocation