怎么用脚本获取启用功能列表

wen 实用脚本 2

本文目录导读:

怎么用脚本获取启用功能列表

  1. PowerShell 脚本(推荐)
  2. 批处理脚本 (CMD)
  3. VBScript 脚本
  4. Python 脚本
  5. 进阶用法 - 获取详细信息
  6. 注意事项
  7. 常见问题解决

要获取系统的“启用功能列表”(Windows 可选功能/特性),可以使用多种脚本方法,以下是几种常见且有效的方式:

PowerShell 脚本(推荐)

查看所有已启用的 Windows 功能

# 获取所有已启用的 Windows 功能
Get-WindowsOptionalFeature -Online | Where-Object { $_.State -eq 'Enabled' } | Format-Table FeatureName, State

导出到 CSV 文件

Get-WindowsOptionalFeature -Online | 
    Where-Object { $_.State -eq 'Enabled' } | 
    Select-Object FeatureName, State, Description |
    Export-Csv -Path "C:\enabled_features.csv" -NoTypeInformation

简化的显示方式

Get-WindowsOptionalFeature -Online | 
    Where-Object State -eq Enabled | 
    Select-Object FeatureName

批处理脚本 (CMD)

@echo off
echo 正在获取已启用的 Windows 功能...
echo.
REM 方法1:使用 DISM 命令
dism /online /get-features /format:table | findstr /i "启用"
REM 或者保存到文件
dism /online /get-features > "%TEMP%\features.txt"
findstr /i "启用" "%TEMP%\features.txt"
echo.
echo 结果已保存到 %TEMP%\features.txt

VBScript 脚本

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("dism /online /get-features /format:table")
Set objStdOut = objExec.StdOut
Dim strLine
Do While Not objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine()
    If InStr(strLine, "启用") > 0 Then
        WScript.Echo strLine
    End If
Loop

Python 脚本

import subprocess
import re
def get_enabled_features():
    # 运行 DISM 命令
    result = subprocess.run(
        ["dism", "/online", "/get-features", "/format:table"],
        capture_output=True,
        text=True,
        encoding='utf-8'
    )
    enabled_features = []
    for line in result.stdout.split('\n'):
        if '启用' in line or 'Enabled' in line:
            # 提取功能名称
            match = re.match(r'([\w\.\-]+)\s+\|\s+(.*)', line)
            if match:
                feature_name = match.group(1).strip()
                enabled_features.append(feature_name)
    return enabled_features
# 使用示例
features = get_enabled_features()
print("已启用的 Windows 功能:")
for feature in features:
    print(f"  - {feature}")
# 保存到文件
with open("enabled_features.txt", "w", encoding="utf-8") as f:
    for feature in features:
        f.write(f"{feature}\n")

进阶用法 - 获取详细信息

PowerShell 获取详细属性

# 获取包含详细属性的列表
Get-WindowsOptionalFeature -Online | 
    Where-Object State -eq Enabled |
    Select-Object FeatureName, State, Description, *

获取特定功能组的状态

# 查看.NET Framework 相关功能
Get-WindowsOptionalFeature -Online | 
    Where-Object { $_.FeatureName -like "*NetFx*" -and $_.State -eq 'Enabled' }

注意事项

  1. 管理员权限:大多数方法需要以管理员身份运行
  2. 操作系统版本:不同 Windows 版本可能略有差异
  3. 语言问题:中文系统显示“启用”,英文系统显示“Enabled”

常见问题解决

Get-WindowsOptionalFeature 不可用:

# 安装 DISM 模块
Install-WindowsFeature -Name DISM

获取所有功能(包括禁用)并筛选:

dism /online /get-features /format:table | findstr /i "启用"

推荐使用 PowerShell 方法,因为它提供了最丰富的筛选和格式化选项,且易于集成到自动化流程中。

抱歉,评论功能暂时关闭!