Visual Studio 安裝的命令列參數範例

為了說明如何使用命令列參數安裝 Visual Studio,以下是幾個範例,您可以自訂以符合您的需求。

在每個範例中,vs_enterprise.exevs_professional.exevs_community.exe 表示 Visual Studio 啟動程序的個別版本,這是起始下載程序的小型檔案 (~ 1MB)。 如果您使用不同的版本,請取代為適當的啟動程序名稱。

所有命令都需要提升系統管理權限,如果未從已提升權限的提示字元啟動程序,則會顯示 [使用者帳戶控制] 提示。

您可以在命令列結尾處使用 ^ 字元,將多行串連成單一命令。 或者,您可以將這幾行放到一列。 在 PowerShell 中,對等項目為倒引號 (`) 字元。

如需您可以使用命令列安裝的工作負載和元件的清單,請參閱 Visual Studio 工作負載和元件識別碼頁面。

使用 --installPath 與啟動程序一起安裝

  • 安裝 Visual Studio 的最小執行個體,不會顯示互動式提示,但會顯示進度:

    vs_enterprise.exe --installPath C:\minVS ^
    --add Microsoft.VisualStudio.Workload.CoreEditor ^
    --passive --norestart
    
  • 使用法文語言套件以無訊息方式安裝 Visual Studio 的桌面執行個體,並只在安裝產品時傳回。

    vs_enterprise.exe --installPath C:\desktopVS ^
    --addProductLang fr-FR ^
    --add Microsoft.VisualStudio.Workload.ManagedDesktop ^
    --includeRecommended --quiet --wait
    

更新

  • 使用命令列更新 Visual Studio 執行個體,顯示進度但不顯示互動式提示。 您可以使用在用戶端或配置中找到的啟動程序,以兩個步驟中執行這些系列的命令。 第一個命令會更新用戶端上的安裝程式,而第二個命令會更新 Visual Studio 產品。 您必須在提升權限的命令提示字元中執行這些命令,因為更新安裝程式需要系統管理員權限。 下列範例會模擬在配置中使用常駐啟動程序來更新用戶端。

    \\layoutserver\share\path\vs_enterprise.exe --update --quiet --wait
    \\layoutserver\share\path\vs_enterprise.exe update --wait --passive --norestart --installPath "C:\installPathVS"
    

或者,您也可以使用用戶端上的安裝程式,在一個步驟中更新 Visual Studio 執行個體。 標準使用者若已獲授與適當的權限,可以使用安裝程式以程式設計方式執行更新命令,但不允許使用 --passive--quiet 參數。 請注意,您無法以程式設計方式從安裝程式所在的同一個目錄起始安裝程式。

"C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe" update --passive --norestart --installPath "C:\installPathVS"

使用 --wait

  • 在批次檔或指令碼中使用 --wait,以等候初始命令完成,然後再執行下一個命令。 針對批次檔,%ERRORLEVEL% 環境變數會包含命令的傳回值,如使用命令列參數安裝 Visual Studio 頁面中所述。 有些命令公用程式需要其他參數來等候完成,以及取得安裝程式的傳回值。

只有啟動程序才支援 --wait 參數。

start /wait vs_professional.exe --installPath "C:\VS" --passive --wait > nul
echo %errorlevel%

下列命令是使用 --wait 搭配 PowerShell 指令碼命令 Start-Process 的範例:

$process = Start-Process -FilePath vs_enterprise.exe -ArgumentList "--installPath", "C:\VS", "--passive", "--wait" -Wait -PassThru
Write-Output $process.ExitCode 

 $startInfo = New-Object System.Diagnostics.ProcessStartInfo
 $startInfo.FileName = "vs_enterprise.exe"
 $startInfo.Arguments = "--all --quiet --wait"
 $process = New-Object System.Diagnostics.Process
 $process.StartInfo = $startInfo
 $process.Start()
 $process.WaitForExit()
  • 第一個 --wait 會由 Visual Studio 啟動程序所使用,而第二個 -Wait 會由 'Start-Process' 用來等候完成。 -PassThru 參數是 Start-Process 用來使用安裝程式的結束代碼做為其傳回值。

使用 --layout 建立網路配置或本機快取

  • 建立只包含 Visual Studio 核心編輯器的配置 (最少的 Visual Studio 組態) 和英文語言套件。 如需更多配置建立範例,請參閱建立 Visual Studio 的網路安裝頁面。

    vs_professional.exe --layout "C:\VS" ^
    --lang en-US ^
    --add Microsoft.VisualStudio.Workload.CoreEditor ^
    

使用 --all 來取得整個產品

  • 啟動 Visual Studio Enterprise 版中可用的所有工作負載和元件互動式安裝:

    vs_enterprise.exe --all
    

使用 --includeRecommended

  • 使用暱稱在已安裝 Visual Studio Community 版並支援 Node.js 開發的電腦上安裝第二個 Visual Studio Professional 的執行個體:

    vs_professional.exe --installPath C:\VSforNode ^
    --add Microsoft.VisualStudio.Workload.Node --includeRecommended --nickname VSforNode
    

使用 --channelURI

使用最新的安裝程式,即可設定 Visual Studio 尋找更新的位置。 否則 --channelUri 為更新通道或更新的來源位置。 下表提供 channelId 和 channelUri 的範例值及其意義。

通道名稱 --channelUri --channelId
Visual Studio 2022 目前通道 https://aka.ms/vs/17/release/channel VisualStudio.17.Release
Visual Studio 2022 17.0 LTSC 通道 https://aka.ms/vs/17/release.LTSC.17.0/channel VisualStudio.17.Release.LTSC.17.0
Visual Studio 2022 預覽通道 https://aka.ms/vs/17/pre/channel VisualStudio.17.Preview
Visual Studio 2019 發行通道 https://aka.ms/vs/16/release/channel VisualStudio.16.Release
Visual Studio 2017 發行通道 https://aka.ms/vs/15/release/channel VisualStudio.15.Release
自訂配置 - 私人通道 \\layoutserver\share\path\channelmanifest.json VisualStudio.17.Release (或任何根據的配置)

如果您選擇使用自訂配置作為更新通道,請注意下列指導方針:

使用 --remove

  • 從預設安裝的 Visual Studio 執行個體移除分析工具元件。 此範例會使用已安裝在用戶端電腦上的安裝程式。 標準使用者若有適當的權限,可以使用安裝程式以程式設計方式執行修改命令,但不允許使用 --passive--quiet 參數。 請注意,您無法以程式設計方式從安裝程式所在的同一個目錄起始安裝程式。

    "C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe" modify ^
    --installPath "C:\Program Files\Microsoft Visual Studio\2022\Enterprise" ^
    --remove Microsoft.VisualStudio.Component.DiagnosticTools ^
    --passive
    

您無法在與 --layout 相同的命令中使用 --remove。 換句話說,您無法從配置中移除元件。

使用 --removeOos

使用最新的安裝程式,您可以修改安裝,並從預設安裝的 Visual Studio 執行個體中移除所有已轉換為不支援狀態的元件。 此範例會使用已安裝在用戶端電腦上的安裝程式來設定 removeOos 設定。 標準使用者若已獲授與適當的權限,可以使用安裝程式以程式設計方式執行修改命令,但不允許使用 --passive--quiet 參數。 您無法以程式設計方式從安裝程式所在的同一個目錄起始安裝程式。

 "C:\Program Files (x86)\Microsoft Visual studio\Installer\setup.exe" modify ^
 --installPath "C:\Program Files\Microsoft Visual Studio\2022\Enterprise" ^
 --removeOos true ^
 --passive
  • 調整更新設定,以在每次產品更新時,一致地移除已轉換為不再支援狀態的所有元件:

    "C:\Program Files (x86)\Microsoft Visual studio\Installer\setup.exe" modify ^
    --channelURI https://aka.ms/vs/17/release.LTSC.17.0/channel ^
    --productID Microsoft.VisualStudio.Product.Enterprise ^
    --newChannelURI \\layoutserver\share\path\channelmanifest.json ^
    --removeOos true ^
    --quiet  
    

使用 --path

所有這些範例都假設您正在使用啟動程序來安裝新產品。

  • 使用安裝、快取和共用路徑:

    vs_enterprise.exe --add Microsoft.VisualStudio.Workload.CoreEditor --path install="C:\VS" --path cache="C:\VS\cache" --path shared="C:\VS\shared"

  • 只使用安裝和快取路徑:

    vs_enterprise.exe --add Microsoft.VisualStudio.Workload.CoreEditor --path install="C:\VS" --path cache="C:\VS\cache"

  • 只使用安裝和共用路徑:

    vs_enterprise.exe --add Microsoft.VisualStudio.Workload.CoreEditor --path install="C:\VS" --path shared="C:\VS\shared"

  • 只使用安裝路徑:

    vs_enterprise.exe --add Microsoft.VisualStudio.Workload.CoreEditor --path install="C:\VS"

使用 export

  • 使用匯出可儲存來自安裝的選取項目。 此範例會使用已安裝在用戶端電腦上的安裝程式。

    "C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe" export --installPath "C:\VS" --config "C:\my.vsconfig"
    
  • 使用匯出可從頭開始儲存自訂選取項目。 此範例會使用已安裝在用戶端電腦上的安裝程式。

    "C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe" export --channelId VisualStudio.17.Release --productId Microsoft.VisualStudio.Product.Enterprise --add Microsoft.VisualStudio.Workload.ManagedDesktop --includeRecommended --config "C:\my.vsconfig"
    

使用 --config

  • 使用 --config 可從先前儲存的安裝組態檔安裝工作負載和元件:

    vs_enterprise.exe --config "C:\my.vsconfig" --installPath "C:\VS"
    
  • 使用 --config 可將工作負載和元件新增至現有的安裝。 此範例會使用已安裝在用戶端電腦上的安裝程式。 標準使用者若有適當的權限,可以使用安裝程式以程式設計方式執行修改命令,但不允許使用 --passive--quiet 參數。 您無法以程式設計方式從安裝程式所在的同一個目錄起始安裝程式。

    "C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe" modify --installPath "C:\VS" --config "C:\my.vsconfig"
    
  • 使用 --config 來設定配置的內容:

    vs_enterprise.exe --layout C:\layout --config "C:\my.vsconfig" 
    

使用 winget

使用 Windows 封裝管理員 "winget" 工具,以程式設計方式在機器上安裝或更新 Visual Studio,以及其他由 winget 管理的封裝。 若要自訂安裝並指定其他工作負載和元件,您可以使用 winget 的 --override 參數與 winget 的 install 命令,並傳入匯出的 vsconfig 檔案,如下所示:

winget install --id Microsoft.VisualStudio.2022.Community --override "--passive --config C:\my.vsconfig"

您也可以使用 winget configure 並傳入 .yaml 檔案來修改現有的 Visual Studio 安裝。 此方法會使用這裡記載Visual Studio PowerShell DSC 提供者

支援或疑難排解

有時可能會發生一些問題。 若 Visual Studio 安裝失敗,請參閱針對 Visual Studio 安裝和升級問題進行疑難排解,以取得逐步指導方針。

以下是一些支援選項: