当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 获取 WindowsOptionalFeature

作者:精品下载站 日期:2024-12-14 17:26:17 浏览:13 分类:玩电脑

PowerShell 获取 WindowsOptionalFeature


使用 PowerShell 中的 Get-WindowsOptionalFeature cmdlet 获取有关所有功能的信息,包括 Windows 映像中的可选功能。

Get-WindowsOptionalFeature 用于获取正在运行的操作系统中的 Windows 可选功能列表、检索包中的可选功能列表以及获取有关已安装映像中的功能的详细信息。

Get-WindowsOptionalFeature 语法包含可用于指定功能名称、包名称、在线、包路径、路径、Windows 目录和日志级别的参数。

在本文中,我们将讨论如何在 PowerShell 中使用 Get-WindowsOptionalFeature cmdlet 来获取有关所有功能的信息。

酷提示:Get-WindowsOptionalFeature 需要提升。您必须以“以管理员身份运行”身份打开 PowerShell 终端才能运行 cmdlet。

Get-WindowsOptionalFeature 的语法是什么

Get-WindowsOptionalFeature 的语法如下所示。

Get-WindowsOptionalFeature
   [-FeatureName <String>]
   [-PackageName <String>]
   [-PackagePath <String>]
   [-Online]
   -Path <String>
   [-WindowsDirectory <String>]
   [-SystemDrive <String>]
   [-LogPath <String>]
   [-ScratchDirectory <String>]
   [-LogLevel <LogLevel>]
   [<CommonParameters>]

下面给出了 Get-WindowsOptionalFeature 中使用的参数

-FeatureName:获取详细信息的功能名称。功能名称区分大小写。

-PackageName: Windows 映像中列出的包的名称。使用 -PackageName 参数获取包中的所有功能。您可以使用 FeatureName 和 PackageName 参数来获取有关包中特定功能的更多详细信息。

-PackagePath: 它指定Windows 映像中.cab 文件的名称。使用此参数可以获取包中的所有功能。

-路径: 它指定您将提供服务的脱机 Windows 映像的根目录的完整路径。

-Online: 使用 Online 参数指定本地计算机上正在运行的操作系统。

-LogPath:它指定要登录的完整路径和文件名。如果未设置,则它使用默认路径 %WINDIR%\Logs\Dism\dism.log

-ScratchDirectory:它指定在服务期间提取文件以供使用时使用的临时目录。该目录必须存在于本地。如果未指定,它将使用%Windows\%Temp%

让我们通过示例了解 DISM 模块中的 PowerShell Get-WindowsOptionalFeature cmdlet。在下面的示例中,我们将使用该命令获取包中的功能列表,并获取有关已安装映像中的功能的详细信息。

如何列出正在运行的操作系统中的可选功能

使用 PowerShell 中的 Get-WindowsOptionalFeature cmdlet 获取操作系统中可选功能的列表。

Get-WindowsOptionalFeature -Online

在上面的 PowerShell 脚本中,命令 Get-WindowsOptionalFeature 使用参数 -Online 列出正在运行的 Windows 操作系统中的所有可选功能。

上述命令的输出检索所有功能的列表,并显示正在运行的操作系统中所有功能的功能名称和状态(启用或禁用)。

[玩转系统] PowerShell 获取 WindowsOptionalFeature

仅列出正在运行的操作系统中启用的可选功能

Windows 操作系统包含启用或禁用的可选功能。您可以使用过滤器来获取正在运行的操作系统中已启用的可选功能的列表,以检查状态是否已启用。

Get-WindowsOptionalFeature -Online | Where {$_.state -eq 'Enabled'} | Select FeatureName 

在上面的 PowerShell 脚本中,Get-WindowsOptionalFeature cmdlet 使用 -Online 参数列出所有可用功能,并使用管道运算符将其发送到下一个命令。

Where 条件检查功能的状态是否等于“已启用”并选择“FeatureName”。

上述命令的输出显示已启用的可选功能的列表。

仅列出正在运行的操作系统中禁用的可选功能

您可以使用 Get-WindowsOptionalFeatures 和 where 条件检索正在运行的操作系统中禁用的可选功能,以检查功能状态是否已禁用。

Get-WindowsOptionalFeature -Online | Where {$_.state -eq 'Disabled'} | Select FeatureName  

在上面的 PowerShell 脚本中,Get-WindowsOptionalFeature 命令使用参数 -Online 获取所有可选功能的列表,并使用 Where 条件检查状态是否禁用,并显示功能名称。

它显示正在运行的操作系统中禁用的可选功能。

获取有关已安装图像中的功能的详细信息

使用带有参数 -Path 和 -FeatureName 的 Get-WindowsOptionalFeature cmdlet 来获取有关已装载映像中的功能的详细信息。

Get-WindowsOptionalFeature -Path "D:\Win11" -FeatureName Hearts

在上面的 PowerShell 脚本中,cmdlet Get-WindowsOptionalFeature 使用 -Path 参数指定脱机 Windows 映像的根目录的路径。它获取有关已安装映像中名为 Hearts 的功能的详细信息。

获取有关已安装映像中指定包中的功能的详细信息

Get-WindowsOptionalFeature -Path "D:\Win11" -FeatureName "Hearts" -PackagePath "C:\packages\package.cab"

在上面的 PowerShell 脚本中,Get-WindowsOptionalFeature 使用 Path 参数指定挂载到 D:\Win11 的 Windows 映像的路径,它显示有关 c:\packages\package.cab 包中的 Hearts 功能的详细信息。

酷提示:如何使用 PowerShell 和 Cmd 安装 telnet 客户端!

使用通配符获取有关功能的详细信息

您可以使用通配符在命令中指定功能名称以获取有关它的详细信息。

Get-WindowsOptionalFeature -Online -FeatureName *Hyper-V*

在上述 PowerShell 脚本中,Get-WindowsOptionalFeature 使用 -Online 参数列出所有可选功能并检查功能名称*Hyper-V* 在其中。

它返回有关正在运行的操作系统中的 Hyper-V 可选功能的详细信息。

用于获取有关 Hyper-v 功能名称详细信息的上述脚本的输出为:

PS C:\> Get-WindowsOptionalFeature -Online -FeatureName *Hyper-V*                                                       

FeatureName      : Microsoft-Hyper-V-All
DisplayName      : Hyper-V
Description      : Provides services and management tools for creating and running virtual machines and their
                   resources.
RestartRequired  : Possible
State            : Disabled
CustomProperties :


FeatureName      : Microsoft-Hyper-V
DisplayName      : Hyper-V Platform
Description      : Provides the services that you can use to create and manage virtual machines and their resources.
RestartRequired  : Possible
State            : Disabled
CustomProperties :

您可以使用 Get-WindowsOptionalFeature cmdlet 通过 PowerShell 检查正在运行的操作系统中是否启用了 hyper-v。

获取远程计算机的 Windows 可选功能

您可以使用 Invoke-Command cmdlet 获取有关远程计算机上可选功能的信息。

以下 PowerShell 脚本将获取有关名为“incorp-eu-101”的远程计算机上的可选功能的信息

# Specify the remote computer name

$remoteComputerName = "incorp-eu-101"

# Get the list of optional features on remote computer

$features = Invoke-Command -ComputerName $remoteComputerName {Get-WindowsOptionalFeature -Online}

# Print the list of optional features
$features  

上述 PowerShell 脚本使用 Invoke-Command cmdlet 在远程计算机上运行 Get-WindowsOptionalFeature cmdlet,检索所有可选功能的列表并将其存储在变量 中$功能。最后,它将可选功能列表打印到控制台。

在 Windows 操作系统上获取 Windows 可选功能 RSAT 状态

RSAT(远程服务器管理工具)可以安装在 Windows 10/Windows 11 或 Windows Server 系统上。要在安装之前检查 Windows 操作系统上的 RSAT 状态,请运行以下 PowerShell 脚本。

Get-WindowsOptionalFeature -Online -FeatureName *RSAT* | Select-Object FeatureName, state

在上面的 PowerShell 脚本中,Get-WindowsOptionalFeature cmdlet 获取正在运行的操作系统(此处为 Windows 10)上有关 RSAT 的详细信息,并显示其功能名称和状态。

获取 Windows 操作系统上的 Windows 可选功能 Internet Explorer 状态

您可以使用 PowerShell 中的 Get-WindowsOptionalFeature cmdlet 来获取功能名称“Internet Explorer”详细信息及其在 Windows 操作系统上的状态。

Get-WindowsOptionalFeature -Online -FeatureName *Internet-Explorer* | Select-Object FeatureName,State

在上面的 PowerShell 脚本中,Get-WindowsOptionalFeature 命令检索“Internet-Explorer”功能的详细信息,并在给定的 Windows 10 操作系统上将其状态显示为“已启用”。

上述 PowerShell 脚本的输出是:

PS C:\> Get-WindowsOptionalFeature -Online -FeatureName *Internet-Explorer* | Select-Object FeatureName,State

FeatureName                        State
-----------                        -----
Internet-Explorer-Optional-amd64 Enabled

如何在Windows操作系统上检查SMB1协议状态?

使用 Get-WindowsOptionalFeature cmdlet 获取有关 SMB1Protocol 功能的详细信息。它将获取 SMB1Protocol 功能名称、显示名称、描述和状态。

Get-WindowsOptionalFeature -Online -FeatureName SMB1Protocol

在上述 PowerShell 脚本中,Get-WindowsOptionalFeature cmdlet 使用 FeatureName 参数指定 SMB1Protocol 名称并检索其属性。

上述脚本的输出在 Windows 10 系统(正在运行的操作系统)上将 SMB1Protocol 状态显示为“已禁用”。

PS C:\> Get-WindowsOptionalFeature -Online -FeatureName SMB1Protocol


FeatureName      : SMB1Protocol
DisplayName      : SMB 1.0/CIFS File Sharing Support
Description      : Support for the SMB 1.0/CIFS file sharing protocol, and the Computer Browser protocol.
RestartRequired  : Possible
State            : Disabled
CustomProperties :
                   ServerComponent\Description : Support for the SMB 1.0/CIFS file sharing protocol, and the Computer Browser protocol.
                   ServerComponent\DisplayName : SMB 1.0/CIFS File Sharing Support
                   ServerComponent\Id : 487
                   ServerComponent\Type : Feature
                   ServerComponent\UniqueName : FS-SMB1
                   ServerComponent\Deploys\Update\Name : SMB1Protocol

结论

我希望上述有关 PowerShell Get-WindowsOptionalFeature 主题的文章列出所有可选功能对您有所帮助。

我们学习了如何在 PowerShell 中使用 cmdlet Get-WindowsOptionalFeature 列出所有可选功能、获取有关正在运行的操作系统中的功能的详细信息以及使用不同参数安装的映像。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯