powershell 是管理 windows 角色与功能最高效的方式,windows server 使用 servermanager 模块(如 get-windowsfeature、install-windowsfeature),windows 10/11 则用 dism 命令(如 dism /online /get-features);两者均支持查看、安装、卸载及批量部署角色与功能。
powershell 是管理 windows 角色与功能最高效的方式,尤其适用于服务器(如 windows server)和启用了“windows 功能”组件的桌面系统。核心命令基于 servermanager 模块(windows server)或 dism 模块(windows 10/11),不同系统需区分使用。
查看当前已安装的角色与功能
在 Windows Server 上,运行以下命令列出所有已启用/已安装的角色与功能:
> Get-WindowsFeature | Where-Object Installed
若想同时看到状态(已安装、可用但未安装、不可用),可执行:
> Get-WindowsFeature | Select-Object Name, DisplayName, Installed, InstallState
对于 Windows 10/11(无 ServerManager 模块),改用 DISM:
> dism /online /get-features /format:table
或导出为详细列表:
> dism /online /get-features /format:list
安装或启用指定角色或功能
在 Windows Server 中,使用 Install-WindowsFeature 命令(支持名称、显示名或通配符):
- 按功能名安装: Install-WindowsFeature -Name Web-Server
- 启用带管理工具的完整 Web 服务器: Install-WindowsFeature -Name Web-Server -IncludeManagementTools
- 批量安装多个: Install-WindowsFeature -Name Telnet-Client, RSAT-AD-PowerShell
Windows 10/11 使用 DISM 启用可选功能(例如 .NET Framework 或 OpenSSH):
> dism /online /enable-feature /featurename:NetFx3 /All /LimitAccess
> dism /online /enable-feature /featurename:OpenSSH.Server /All
注意:/All 表示同时启用依赖项;/LimitAccess 防止从 Windows Update 下载源文件(需本地源时使用)。
卸载或禁用角色与功能
Windows Server 中使用 Uninstall-WindowsFeature:
- 仅卸载功能本身(保留管理工具): Uninstall-WindowsFeature -Name Web-Server
- 彻底移除含管理工具: Uninstall-WindowsFeature -Name Web-Server -Remove
- 卸载后重启(如需):添加
-Restart参数
Windows 10/11 中禁用功能:
> dism /online /disable-feature /featurename:TelnetClient
某些功能(如 WSL2、Hyper-V)禁用后需重启才生效,DISM 不自动重启,需手动执行 shutdown /r /t 0(谨慎操作)。
导出与导入角色/功能配置(适合批量部署)
在一台已配置好的 Windows Server 上,可导出当前角色与功能状态为 XML 文件:
> Get-WindowsFeature | Where-Object Installed | Export-Clixml C:\config\features-installed.xml
在其他服务器上还原(仅安装已导出的已启用项):
> Import-Clixml C:\config\features-installed.xml | ForEach-Object { Install-WindowsFeature -Name $_.Name -IncludeManagementTools }
该方法不覆盖差异,也不处理依赖冲突,建议先测试。生产环境推荐配合 Desired State Configuration(DSC)或 Ansible 等工具做一致性管控。











