以命令行方式安装Windows功能,居然可以有这么多不同的命令!

前一阵我在搜集相关素材用以完成 《如何离线安装XPS查看器》 一文时,偶然搜索到了一篇老外的博客。

这篇博客挺有意思,讲的是使用命令行方式安装Windows功能,可以通过多种不同的命令来实现。

这引起了我的好奇,于是我把它的内容翻译加工并总结了一下,分享给小伙伴儿们。

老外博客链接:https://hahndorf.eu/blog/WindowsFeatureViaCmd


正式开始之前,先来啰嗦几句,说说 Windows 功能 是指什么。

在 Window7 系统中它被称作 打开或关闭 Windows 功能


而在 Windows10 系统中它则被称作 启用或关闭 Windows 功能


同时,Windows10 系统中还有另一个叫作 可选功能 的新东东。


好了,可以开始了。

通常我们都是用鼠标指指点点就完成功能的安装和卸载的。

但是,要想完美地使用命令行来安装Windows功能,以下命令你会选择哪一个呢?

  • Add-WindowsCapability
  • Enable-WindowsOptionalFeature
  • Install-WindowsFeature
  • Add-WindowsFeature
  • dism.exe
  • pkgmgr.exe


What F*?你可能要尖叫起来了!

什么鬼,哪儿来这么多命令啊?

嘿嘿别着急,长相相似的它们还是有区别的,接着往下看就是了。


先提一嘴 Add-WindowsCapability ,它是个新来的

先说一点,这个 *-WindowsCapability 样式的命令其实是 Windows 10Server 2016 及其后续版本中新添加的。

它与 Enable-WindowsOptionalFeature 命令功能及其相似,并且更加强大的是,它还可以从 Windows Update 或本地存储库下载程序包。


这些命令分别用在哪些支持平台上呢

Vista+2008 Win7 2008R2 Win8.* 2012R* Win10 2016 Nano Source
Enable-WindowsOptionalFeature * * * * * Dism module
Get-WindowsOptionalFeature * * * * * Dism module
Get-WindowsCapability * * ? Dism module
Install-WindowsFeature * * ServerManager module
Get-WindowsFeature * * * ServerManager module
Add-WindowsFeature * A A ServerManager module
dism.exe * * * * * * * %SystemRoot%\System32
pkgmgr.exe * * * * * * * * %SystemRoot%\System32

A = An alias for Install-WindowsFeature(Install WindowsFeature的别名)


这张表格很重要,从中能看出来,来源是 Server Manager 的命令只能在服务器上使用,或者在工作站上安装远程管理工具时可用。

如果你要在一般的客户端计算机上使用,那么不好意思请使用 Dism 命令。

同时为了彻底的安全起见,请使用 dism.exe

如果你还在用 Vista/Server 2008,那么你还是看看 pkgmgr.exe 吧。


为啥会有两套不同的 PowerShell 命令呢

问得好!

我猜测可能微软存在两支截然不同的团队。

其一服务器管理团队,他们创建了相应的命令行以支持服务器管理中的功能。

另外一支 dism 团队(?),它们创建了 dism.exe 却猛然发现已经另有类似功能的程序,可是他们又不想放弃他们的程序。

其实我看这些命令不止两套,挺乱的,接着往下看。


不同功能

让我们看看命令的详细说明:

PS C:\> Install-WindowsFeature [-Name] <Feature[]> [-ComputerName <String>] [-Credential <PSCredential>]
 [-IncludeAllSubFeature] [-IncludeManagementTools] [-LogPath <String>] [-Restart] [-Source <String[]>] [-Confirm]
 [-WhatIf] [<CommonParameters>]

PS C:\> Install-WindowsFeature [-ComputerName <String>] [-Credential <PSCredential>] [-LogPath <String>] [-Restart]
 [-Source <String[]>] [-Vhd <String>] -ConfigurationFilePath <String> [-Confirm] [-WhatIf] [<CommonParameters>]

PS C:\> Install-WindowsFeature [-Name] <Feature[]> [-ComputerName <String>] [-Credential <PSCredential>]
 [-IncludeAllSubFeature] [-IncludeManagementTools] [-LogPath <String>] [-Source <String[]>] -Vhd <String>
 [-Confirm] [-WhatIf] [<CommonParameters>]
 


和这个比较一下:

PS C:\> Enable-WindowsOptionalFeature [-All] [-LimitAccess] [-LogLevel <LogLevel>] [-LogPath <String>] [-NoRestart]
 [-PackageName <String>] [-ScratchDirectory <String>] [-Source <String[]>] [-SystemDrive <String>]
 [-WindowsDirectory <String>] -FeatureName <String[]> -Online [<CommonParameters>]

PS C:\> Enable-WindowsOptionalFeature [-All] [-LimitAccess] [-LogLevel <LogLevel>] [-LogPath <String>] [-NoRestart]
 [-PackageName <String>] [-ScratchDirectory <String>] [-Source <String[]>] [-SystemDrive <String>]
 [-WindowsDirectory <String>] -FeatureName <String[]> -Path <String> [<CommonParameters>]
 


一个很大的区别是,服务器管理命令可以针对远程计算机工作,而 dism 只能在本地工作,当然它可以针对离线脱机环境的安装。

Install-WindowsFeature 支持一个很棒的技巧,如果您想知道已经安装了哪些其他功能,可以使用 -whatif 开关。

这在使用 *-includealsubfeature 时有效,但在安装角色时,它只列出它将要安装的所有子功能。

同样,UnInstall-WindowsFeature 命令的使用方法相同,你试试看就知道了。

PS C:\> UnInstall-WindowsFeature -Name NET-Framework-45-Features -whatif
 

像这样你可以查看到 .Net Framework 都有哪些子功能而不用真的卸载它。


不同的 Windows 功能名称

两组命令行都各自使用了自己命名的功能名称 :-(。

dism module 命令行使用与其字面意思相同的名称 dism.exe ,所以如果你以前用惯了就可以继续接着用。

服务器管理命令行则使用了不同的名称。

在服务器 2012 R2 系统上执行以下测试。

PS C:\> Get-WindowsFeature | Select Name | Sort Name
 

结果返回 267 项功能。

PS C:\> Get-WindowsOptionalFeature -Online | Select FeatureName | Sort FeatureName
 

结果返回 311 项功能,和下面命令返回的结果完全一样:

PS C:\> Dism.exe -Online -Get-Features
 


Windows功能名称列表

*-WindowsOptionalFeature (Dism) *-WindowsFeature (Server-Manager)
ActiveDirectory-PowerShell
ADCertificateServicesManagementTools
ADCertificateServicesRole
AdminUI
Application-Server
Application-Server-HTTP-Activation
Application-Server-MSMQ-Activation
Application-Server-Pipe-Activation
Application-Server-TCP-Activation
Application-Server-TCP-Port-Sharing
Application-Server-WAS-Support
Application-Server-WebServer-Support
AppServer
AS-Dist-Transaction
AS-Ent-Services
AS-Incoming-Trans
AS-NET-Framework
AS-Outgoing-Trans
AS-WS-Atomic
AuthManager
BdeAducExtTool
BiometricFramework
BitLocker
BitLocker-NetworkUnlock
BitLocker-RemoteAdminTool
Bitlocker-Utilities
BITS
BITSExtensions-AdminPack
BITSExtensions-Upload
BusScan-ScanServer
CCFFilter
CertificateEnrollmentPolicyServer
CertificateEnrollmentServer
CertificateServices
CertificateServicesManagementTools
ClientForNFS-Infrastructure
CoreFileServer
CoreFileServer-RSAT
DamgmtTools
DataCenterBridging
Dedup-Core
DesktopExperience
DfsMgmt
DFSN-Server
DFSR-Infrastructure-ServerEdition
DHCPServer
DHCPServer-Tools
DirectoryServices-ADAM
DirectoryServices-ADAM-Tools
DirectoryServices-AdministrativeCenter
DirectoryServices-DomainController
DirectoryServices-DomainController-Tools
DirectoryServices-ISM-Smtp
DirectPlay
DNS-Server-Full-Role
DNS-Server-Tools
DSC-Service
EnhancedStorage
FailoverCluster-AdminPak
FailoverCluster-AutomationServer
FailoverCluster-CmdInterface
FailoverCluster-FullServer
FailoverCluster-Mgmt
FailoverCluster-PowerShell
FaxServiceConfigRole
FaxServiceRole
FileAndStorage-Services
FileServerVSSAgent
File-Services
File-Services-Search-Service
FRS-Infrastructure
FSRM-Infrastructure
FSRM-Infrastructure-Services
FSRM-Management
Gateway
Gateway-UI
HCAP-Server
HCSRuntime
HCSUI
IAS NT Service
IdentityServer-SecurityTokenService
IIS-ApplicationDevelopment
IIS-ApplicationInit
IIS-ASP
IIS-ASPNET
IIS-ASPNET45
IIS-BasicAuthentication
IIS-CertProvider
IIS-CGI
IIS-ClientCertificateMappingAuthentication
IIS-CommonHttpFeatures
IIS-CustomLogging
IIS-DefaultDocument
IIS-DigestAuthentication
IIS-DirectoryBrowsing
IIS-FTPExtensibility
IIS-FTPServer
IIS-FTPSvc
IIS-HealthAndDiagnostics
IIS-HostableWebCore
IIS-HttpCompressionDynamic
IIS-HttpCompressionStatic
IIS-HttpErrors
IIS-HttpLogging
IIS-HttpRedirect
IIS-HttpTracing
IIS-IIS6ManagementCompatibility
IIS-IISCertificateMappingAuthentication
IIS-IPSecurity
IIS-ISAPIExtensions
IIS-ISAPIFilter
IIS-LegacyScripts
IIS-LegacySnapIn
IIS-LoggingLibraries
IIS-ManagementConsole
IIS-ManagementScriptingTools
IIS-ManagementService
IIS-Metabase
IIS-NetFxExtensibility
IIS-NetFxExtensibility45
IIS-ODBCLogging
IIS-Performance
IIS-RequestFiltering
IIS-RequestMonitor
IIS-Security
IIS-ServerSideIncludes
IIS-StaticContent
IIS-URLAuthorization
IIS-WebDAV
IIS-WebServer
IIS-WebServerManagementTools
IIS-WebServerRole
IIS-WebSockets
IIS-WindowsAuthentication
IIS-WMICompatibility
InkAndHandwritingServices
Internet-Explorer-Optional-amd64
IPAMClientFeature
IPAMServerFeature
iSCSITargetServer
iSCSITargetServer-PowerShell
iSCSITargetStorageProviders
iSNS_Service
KeyDistributionService-PSH-Cmdlets
LegacyComponents
Licensing
Licensing-Diagnosis-UI
Licensing-UI
LightweightServer
ManagementOdata
MediaPlayback
Microsoft-Hyper-V
Microsoft-Hyper-V-Management-Clients
Microsoft-Hyper-V-Management-PowerShell
Microsoft-Hyper-V-Offline
Microsoft-Hyper-V-Online
Microsoft-Windows-Deployment-Services
Microsoft-Windows-Deployment-Services-Admin-Pack
Microsoft-Windows-Deployment-Services-Deployment-Server
Microsoft-Windows-Deployment-Services-Legacy-SIS
Microsoft-Windows-Deployment-Services-Transport-Server
Microsoft-Windows-FCI-Client-Package
Microsoft-Windows-GroupPolicy-ServerAdminTools-Update
MicrosoftWindowsPowerShell
MicrosoftWindowsPowerShellISE
MicrosoftWindowsPowerShellRoot
MicrosoftWindowsPowerShellV2
Microsoft-Windows-ServerEssentials-ServerSetup
Microsoft-Windows-Web-Services-for-Management-IIS-Extension
MSMQ
MSMQ-ADIntegration
MSMQ-DCOMProxy
MSMQ-HTTP
MSMQ-Multicast
MSMQ-RoutingServer
MSMQ-Server
MSMQ-Services
MSMQ-Triggers
MSRDC-Infrastructure
MultipathIo
NetFx3
NetFx3ServerFeatures
NetFx4
NetFx4Extended-ASPNET45
NetFx4ServerFeatures
NetworkDeviceEnrollmentServices
NetworkLoadBalancingFullServer
NetworkLoadBalancingManagementClient
NFS-Administration
NIS
NPAS-Role
NPSManagementTools
OEM-Appliance-OOBE
OnlineRevocationServices
OnlineRevocationServicesManagementTools
P2P-PnrpOnly
PeerDist
PKIClient-PSH-Cmdlets
Printing-AdminTools-Collection
Printing-Client
Printing-Client-Gui
Printing-InternetPrinting-Client
Printing-InternetPrinting-Server
Printing-LPDPrintService
Printing-LPRPortMonitor
Printing-Server-Foundation-Features
Printing-Server-Role
Printing-XPSServices-Features
PSync
QWAVE
RasCMAK
RasRoutingProtocols
RasServerAdminTools
RemoteAccess
RemoteAccessMgmtTools
RemoteAccessPowerShell
RemoteAccessServer
RemoteAssistance
Remote-Desktop-Services
ResumeKeyFilter
RightsManagementServices
RightsManagementServices-AdminTools
RightsManagementServicesManagementTools
RightsManagementServices-Role
RMS-Federation
RPC-HTTP_Proxy
RSAT
RSAT-ADDS-Tools-Feature
RSAT-AD-Tools-Feature
RSAT-Hyper-V-Tools-Feature
RSAT-NIS
RSAT-RDS-Tools-Feature
SBMgr-UI
SearchEngine-Server-Package
Security-SPP-Vmw
ServerCore-Drivers-General
ServerCore-EA-IME
ServerCore-EA-IME-WOW64
ServerCore-FullServer
ServerCore-WOW64
Server-Drivers-General
Server-Drivers-Printers
ServerForNFS-Infrastructure
Server-Gui-Mgmt
Server-Gui-Shell
ServerManager-Core-RSAT
ServerManager-Core-RSAT-Feature-Tools
ServerManager-Core-RSAT-Role-Tools
Server-Manager-RSAT-File-Services
ServerMediaFoundation
ServerMigration
Server-Psh-Cmdlets
Server-RSAT-SNMP
ServicesForNFS-ServerAndClient
SessionDirectory
SimpleTCP
SIS-Limited
SMB1Protocol
SMBBW
SmbDirect
SMBHashGeneration
SmbWitness
Smtpsvc-Admin-Update-Name
Smtpsvc-Service-Update-Name
SNMP
Storage-Services
TelnetClient
TelnetServer
TFTP
TIFFIFilter
TlsSessionTicketKey-PSH-Cmdlets
UpdateServices
UpdateServices-API
UpdateServices-Database
UpdateServices-RSAT
UpdateServices-Services
UpdateServices-UI
UpdateServices-WidDatabase
User-Interfaces-Infra
VmHostAgent
VolumeActivation-Full-Role
WAS-ConfigurationAPI
WAS-NetFxEnvironment
WAS-ProcessModel
WAS-WindowsActivationService
WCF-HTTP-Activation
WCF-HTTP-Activation45
WCF-MSMQ-Activation45
WCF-NonHTTP-Activation
WCF-Pipe-Activation45
WCF-Services45
WCF-TCP-Activation45
WCF-TCP-PortSharing45
WebAccess
Web-Application-Proxy
WebEnrollmentServices
WindowsFeedbackForwarder
Windows-Identity-Foundation
Windows-Internal-Database
WindowsMediaPlayer
WindowsPowerShellWebAccess
WindowsServerBackup
WindowsServerBackupSnapin
WindowsStorageManagementService
WINSRuntime
WINS-Server-Tools
WirelessNetworking
WMISnmpProvider
WorkFolders-Server
WSS-Product-Package
Xps-Foundation-Xps-Viewer
AD-Certificate
ADCS-Cert-Authority
ADCS-Device-Enrollment
ADCS-Enroll-Web-Pol
ADCS-Enroll-Web-Svc
ADCS-Online-Cert
ADCS-Web-Enrollment
AD-Domain-Services
ADFS-Federation
ADLDS
ADRMS
ADRMS-Identity
ADRMS-Server
Application-Server
AS-Dist-Transaction
AS-Ent-Services
AS-HTTP-Activation
AS-Incoming-Trans
AS-MSMQ-Activation
AS-Named-Pipes
AS-NET-Framework
AS-Outgoing-Trans
AS-TCP-Activation
AS-TCP-Port-Sharing
AS-WAS-Support
AS-Web-Support
AS-WS-Atomic
Biometric-Framework
BitLocker
BitLocker-NetworkUnlock
BITS
BITS-Compact-Server
BITS-IIS-Ext
BranchCache
CMAK
Data-Center-Bridging
Desktop-Experience
DHCP
DirectAccess-VPN
Direct-Play
DNS
DSC-Service
EnhancedStorage
Failover-Clustering
Fax
FileAndStorage-Services
File-Services
FS-BranchCache
FS-Data-Deduplication
FS-DFS-Namespace
FS-DFS-Replication
FS-FileServer
FS-iSCSITarget-Server
FS-NFS-Service
FS-Resource-Manager
FS-SMB1
FS-SMBBW
FS-SyncShareService
FS-VSS-Agent
GPMC
Hyper-V
Hyper-V-PowerShell
Hyper-V-Tools
InkAndHandwritingServices
Internet-Print-Client
IPAM
IPAM-Client-Feature
iSCSITarget-VSS-VDS
ISNS
LPR-Port-Monitor
ManagementOdata
Migration
MSMQ
MSMQ-DCOM
MSMQ-Directory
MSMQ-HTTP-Support
MSMQ-Multicasting
MSMQ-Routing
MSMQ-Server
MSMQ-Services
MSMQ-Triggers
Multipath-IO
NET-Framework-45-ASPNET
NET-Framework-45-Core
NET-Framework-45-Features
NET-Framework-Core
NET-Framework-Features
NET-HTTP-Activation
NET-Non-HTTP-Activ
NET-WCF-HTTP-Activation45
NET-WCF-MSMQ-Activation45
NET-WCF-Pipe-Activation45
NET-WCF-Services45
NET-WCF-TCP-Activation45
NET-WCF-TCP-PortSharing45
NFS-Client
NLB
NPAS
NPAS-Health
NPAS-Host-Cred
NPAS-Policy-Server
PNRP
PowerShell
PowerShell-ISE
PowerShellRoot
PowerShell-V2
Print-Internet
Print-LPD-Service
Print-Scan-Server
Print-Server
Print-Services
qWave
RDC
RDS-Connection-Broker
RDS-Gateway
RDS-Licensing
RDS-Licensing-UI
RDS-RD-Server
RDS-Virtualization
RDS-Web-Access
RemoteAccess
Remote-Assistance
Remote-Desktop-Services
Routing
RPC-over-HTTP-Proxy
RSAT
RSAT-AD-AdminCenter
RSAT-ADCS
RSAT-ADCS-Mgmt
RSAT-ADDS
RSAT-ADDS-Tools
RSAT-ADLDS
RSAT-AD-PowerShell
RSAT-ADRMS
RSAT-AD-Tools
RSAT-Bits-Server
RSAT-Clustering
RSAT-Clustering-AutomationServer
RSAT-Clustering-CmdInterface
RSAT-Clustering-Mgmt
RSAT-Clustering-PowerShell
RSAT-CoreFile-Mgmt
RSAT-DFS-Mgmt-Con
RSAT-DHCP
RSAT-DNS-Server
RSAT-Fax
RSAT-Feature-Tools
RSAT-Feature-Tools-BitLocker
RSAT-Feature-Tools-BitLocker-BdeAducExt
RSAT-Feature-Tools-BitLocker-RemoteAdminTool
RSAT-File-Services
RSAT-FSRM-Mgmt
RSAT-Hyper-V-Tools
RSAT-NFS-Admin
RSAT-NIS
RSAT-NLB
RSAT-NPAS
RSAT-Online-Responder
RSAT-Print-Services
RSAT-RDS-Gateway
RSAT-RDS-Licensing-Diagnosis-UI
RSAT-RDS-Tools
RSAT-RemoteAccess
RSAT-RemoteAccess-Mgmt
RSAT-RemoteAccess-PowerShell
RSAT-Role-Tools
RSAT-SMTP
RSAT-SNMP
RSAT-VA-Tools
RSAT-WINS
Search-Service
ServerEssentialsRole
Server-Gui-Mgmt-Infra
Server-Gui-Shell
Server-Media-Foundation
Simple-TCPIP
SMTP-Server
SNMP-Service
SNMP-WMI-Provider
Storage-Services
Telnet-Client
Telnet-Server
TFTP-Client
UpdateServices
UpdateServices-API
UpdateServices-DB
UpdateServices-RSAT
UpdateServices-Services
UpdateServices-UI
UpdateServices-WidDB
User-Interfaces-Infra
VolumeActivation
WAS
WAS-Config-APIs
WAS-NET-Environment
WAS-Process-Model
WDS
WDS-AdminPack
WDS-Deployment
WDS-Transport
Web-App-Dev
Web-AppInit
Web-Application-Proxy
Web-ASP
Web-Asp-Net
Web-Asp-Net45
Web-Basic-Auth
Web-Cert-Auth
Web-CertProvider
Web-CGI
Web-Client-Auth
Web-Common-Http
Web-Custom-Logging
Web-DAV-Publishing
Web-Default-Doc
Web-Digest-Auth
Web-Dir-Browsing
Web-Dyn-Compression
Web-Filtering
Web-Ftp-Ext
Web-Ftp-Server
Web-Ftp-Service
Web-Health
Web-Http-Errors
Web-Http-Logging
Web-Http-Redirect
Web-Http-Tracing
Web-Includes
Web-IP-Security
Web-ISAPI-Ext
Web-ISAPI-Filter
Web-Lgcy-Mgmt-Console
Web-Lgcy-Scripting
Web-Log-Libraries
Web-Metabase
Web-Mgmt-Compat
Web-Mgmt-Console
Web-Mgmt-Service
Web-Mgmt-Tools
Web-Net-Ext
Web-Net-Ext45
Web-ODBC-Logging
Web-Performance
Web-Request-Monitor
Web-Scripting-Tools
Web-Security
Web-Server
Web-Stat-Compression
Web-Static-Content
Web-Url-Auth
Web-WebServer
Web-WebSockets
Web-WHC
Web-Windows-Auth
Web-WMI
WFF
Windows-Identity-Foundation
Windows-Internal-Database
WindowsPowerShellWebAccess
Windows-Server-Backup
WindowsStorageManagementService
Windows-TIFF-IFilter
WinRM-IIS-Ext
WINS
Wireless-Networking
WoW64-Support
XPS-Viewer


dism.exePowerShell

如果你在较旧版本的Windows上不使用 PowerShell 的话,那么可以使用 dism.exe 来安装可选功能。

下面是一些 PowerShell 代码,用于检查是否安装了某项功能。

$tempFile = "$env:temp\tempName.log"
& dism.exe /online /get-features /format:table | out-file $tempFile -Force       
$WinFeatures = (Import-CSV -Delim '|' -Path $tempFile -Header Name,state | Where-Object {$_.State -eq "Enabled "}) | Select Name
Remove-Item -Path $tempFile 
 

前面你也已经看到了 所有Windows功能名称列表 ,这时你可以通过以下演示代码检查是否存在这样的功能。

if(($WinFeatures | Where-Object {$_.Name.Trim() -eq "WirelessNetworking"}) -eq $null) {...}
 


最后总结性发言

说了这么多,其实可以总结为以下几点。

1、*-WindowsOptionalFeature (Dism) 用于客户端计算机。

2、*-WindowsFeature (Server-Manager) 用于服务端计算机。

3、*-WindowsCapability 用于 Windows10 系统。

4、dism.exe (Dism)可以用于任何计算机,包括客户端和服务端,包括旧版本的 Windows。


嘿嘿,小伙伴儿们是不是还是有点头晕呢?

说实话,反正我现在还晕着呢。

总之一句话,具体用哪个命令随时可以参考前面说的 “命令支持平台列表” 那张表格,而 dism.exe 命令则是万能的。

OK,小伙伴儿们,你们 Get 到了吗?


WeChat@网管小贾 | www.sysadm.cc



提交评论

安全码
刷新

© 2020-present 网管小贾 | 微信公众号 @网管小贾
许可协议:CC-BY-NC 4.0 | 转载文章请注明作者出处及相关链接