找回密码
 新建账号

用命令行 cmd 或 Powershell 安装 Windows 功能

[复制链接]
蔡徐坤 发表于 2023/12/6 17:55 | 显示全部楼层 |阅读模式
本文解决两个问题,一是用命令行安装 Windows 功能,二是用命令行查询 Windows 功能,列出 Windows 功能。
Windows 除了常规功能,还有很多可选的 Windows 功能可以选择,在 Windows Server 上,可以使用 Powershell Get-WindowsFeature 找到操作系统中的 Windows 功能,使用 Install-WindowsFeature 和 Uninstall-WindowsFeature 安装 Windows 功能,在非服务器版本的 Windows 桌面操作系统中,没有 Get-WindowsFeature Install-WindowsFeature Uninstall-WindowsFeature 命令,如果是 Windows 10+ 可以使用 Powershell 的 Get-WindowsOptionalFeature, Enable-WindowsOptionalFeature, Disable-WindowsOptionalFeature 命令来查看、开启、关闭 Optional Windows Features,早期版本可以运行 optionalFeatures.exe,但它是一个图形界面程序,需要交互操作,如果想要使用命令行 cmd 或者 Powershell 安装 Windows 功能,比如用命令行安装 IIS,可以使用 pkgmgr.exe,从 Windows Vista 和 Windows Server 2008 开始,Windows 系统支持 pkgmgr.exe 命令,这个程序保存在 %SystemDrive%\Windows\System32\pkgmgr.exe,以下命令可以实现用命令行安装 IIS,命令需要管理员权限。
  1. start /w %SystemDrive%\Windows\System32\pkgmgr.exe /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-HttpErrors;IIS-ApplicationDevelopment;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-WebServerManagementTools;IIS-StaticContent;IIS-CGI;IIS-ManagementConsole
复制代码
pkgmgr.exe 不支持列出 Windows 功能,只能安装或者卸载 Windows 功能,想要列出 Windows 功能,可以使用 dism.exe 实现。需要注意的是 dism 返回的 Windows 功能和 Powershell Get-WindowsFeature 返回的 Windows 功能名称不相同,不可以交换使用。
  1. dism.exe /online /Get-Features /format:table
复制代码
在 Windows 7 中以上命令得到了下面这些 Windows 功能。
  1. WindowsRemoteManagement, OEMHelpCustomization, CorporationHelpCustomization, SimpleTCP, SNMP, WMISnmpProvider, TelnetServer, TelnetClient, WindowsGadgetPlatform, InboxGames, More, Solitaire, SpiderSolitaire, Hearts, FreeCell, Minesweeper, PurblePlace, Chess, Shanghai, Internet, Internet, Internet, Internet, IIS-WebServerRole, IIS-WebServer, IIS-CommonHttpFeatures, IIS-HttpErrors, IIS-HttpRedirect, IIS-ApplicationDevelopment, IIS-Security, IIS-URLAuthorization, IIS-RequestFiltering, IIS-NetFxExtensibility, IIS-HealthAndDiagnostics, IIS-HttpLogging, IIS-LoggingLibraries, IIS-RequestMonitor, IIS-HttpTracing, IIS-IPSecurity, IIS-Performance, IIS-HttpCompressionDynamic, IIS-WebServerManagementTools, IIS-ManagementScriptingTools, IIS-IIS6ManagementCompatibility, IIS-Metabase, WAS-WindowsActivationService, WAS-ProcessModel, WAS-NetFxEnvironment, WAS-ConfigurationAPI, IIS-HostableWebCore, IIS-ISAPIExtensions, IIS-ISAPIFilter, IIS-StaticContent, IIS-DefaultDocument, IIS-DirectoryBrowsing, IIS-WebDAV, IIS-ASPNET, IIS-ASP, IIS-CGI, IIS-ServerSideIncludes, IIS-CustomLogging, IIS-BasicAuthentication, IIS-HttpCompressionStatic, IIS-ManagementConsole, IIS-ManagementService, IIS-WMICompatibility, IIS-LegacyScripts, IIS-LegacySnapIn, IIS-FTPServer, IIS-FTPSvc, IIS-FTPExtensibility, IIS-WindowsAuthentication, IIS-DigestAuthentication, IIS-ClientCertificateMappingAuthentication, IIS-IISCertificateMappingAuthentication, IIS-ODBCLogging, MediaPlayback, WindowsMediaPlayer, MediaCenter, OpticalMediaDisc, NetFx3, WCF-HTTP-Activation, WCF-NonHTTP-Activation, RasRip, MSMQ-Container, MSMQ-Server, MSMQ-Triggers, MSMQ-ADIntegration, MSMQ-HTTP, MSMQ-Multicast, MSMQ-DCOMProxy, TabletPCOC, ServicesForNFS-ClientOnly, ClientForNFS-Infrastructure, NFS-Administration, SUA, Printing-Foundation-Features, Printing-Foundation-LPRPortMonitor, Printing-Foundation-LPDPrintService, Printing-Foundation-InternetPrinting-Client, FaxServicesClientPackage, ScanManagementConsole, TFTP, MSRDC-Infrastructure, Printing-XPSServices-Features, Indexing-Service-Package, Xps-Foundation-Xps-Viewer, TIFFIFilter, SearchEngine-Client-Package, Internet-Explorer-Optional-amd64, ManagementOdata
复制代码
实际上,DISM 可以实现 pkgmgr.exe 的所有功能,Windows 7 已经支持 DISM.exe了,新版本的 Windows 操作系统已经不建议使用 pkgmgr.exe,应当换用 DISM,对于 /Enable-Feature 和 /Disable-Feature,需要使用 /FeatureName:xxxx 指定 Windows 功能的名称,如果有多个 Windows 功能,需要使用多个 /FeatureName 参数。
  1. DISM /Online /Get-Features
复制代码
  1. DISM /Online /Enable-Feature
复制代码
  1. DISM /Online /Disable-Feature
复制代码
具体用法可以参考下面的微软官方文档。
参考链接 Windows Package Manager DISM Powershell DISM 命令

手机版|轻松E站

GMT+8, 2024/4/28 18:29

快速回复 返回顶部 返回列表