為了說明 如何使用命令行參數來安裝Visual Studio,以下是數個範例,您可以自定義以符合您的需求。
在每個範例中,vs_enterprise.exe
、vs_professional.exe
和 vs_community.exe
代表個別版本的Visual Studio啟動載入器,這是起始下載程式的小型 (~ 1MB) 檔案。 如果您使用不同的版本,請替換適當的啟動器名稱。
所有命令都需要管理員權限提升,如果程序不是在提升權限的提示下啟動,則會顯示使用者帳戶控制警示。
您可以使用命令行結尾的 ^
字元,將多行串連成單一命令。 或者,您可以將這些行放在單一數據列上。 在 PowerShell 中,等同符號是反引號(`
)字元。
若要以非互動式方式執行命令,您可以使用--passive
或 --quiet
。 如需這些參數的詳細資訊,請參閱 命令和命令行參數 頁面。
如需您可以使用命令行安裝的工作負載和元件清單,請參閱 Visual Studio 工作負載和元件識別碼 頁面。
使用 --installPath 和 --add 參數來與引導程式一起安裝
安裝 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實例。
Standard 使用者,如果已獲授與適當的許可權,可以透過程式使用安裝程式執行更新命令,但不可使用 --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%
下列命令範例示範如何使用 PowerShell 腳本命令 --wait
搭配 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()
- Visual Studio 啟動載入器會使用第一個
--wait
,而 'Start-Process' 會使用第二個-Wait
等候完成。-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 版本的電腦上,使用暱稱安裝第二個 Visual Studio Professional 實例,並支援 Node.js 開發:
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 (或其他所依據的版面配置) |
如果您選擇使用自訂版面設定作為更新通道,請注意下列指導方針:
-
--channelUri
必須指向自定義版面配置中的 『channelmanifest.json』 檔案。 - 系統管理員可以透過設定客戶端的註冊表設定 ,來設定自訂版面配置「私人通道」在更新設定介面中的顯示方式。
使用 --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
您無法在與 --remove
相同的命令中使用 --layout
。 換句話說,您無法從版面配置中移除元件。
使用 --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"
使用匯出
使用匯出從安裝儲存選取範圍。 此範例會使用已安裝在用戶端電腦上的安裝程式。
"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 安裝和升級問題的疑難排解,以獲得逐步指導。
以下是一些更多支援選項:
- 針對安裝相關問題,請使用 安裝聊天(僅限英文)支持選項。
- 請使用 在 Visual Studio 安裝程式和 Visual Studio IDE 中顯示的“報告問題”工具,向我們回報產品問題。 如果您是 IT 系統管理員且未安裝 Visual Studio,則可以提交 IT 系統管理員意見反應。
- 建議功能、追蹤產品問題,並在 Visual Studio 開發人員社群中找到答案。