📜  get-itemproperty select-object (1)

📅  最后修改于: 2023-12-03 15:00:54.120000             🧑  作者: Mango

使用 Get-ItemProperty 和 Select-Object 进行 PowerShell 数据处理

简介

Get-ItemProperty 是 PowerShell 中用于获取系统对象属性的 cmdlet。将其与 Select-Object 结合使用,可以对获取的属性数据进行筛选、排序和过滤,从而获得更加精准的数据。

语法
Get-ItemProperty -Path <string[]> [-Name <string[]>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-UseTransaction] [-ErrorAction <ActionPreference>] [-WarningAction <ActionPreference>] [-InformationAction <ActionPreference>] [-ErrorVariable <string>] [-WarningVariable <string>] [-InformationVariable <string>] [-OutVariable <string>] [-OutBuffer <int>] | Select-Object <property> | <property[]>
参数说明
  • -Path:指定要获取属性的对象路径,可以是文件、目录或注册表项等。
  • -Name:指定要获取的属性名称,可以是一个属性名称,也可以是多个属性名称的数组。
  • -Include:指定要包含的属性名称。
  • -Exclude:指定要排除的属性名称。
  • -Force:在获取对象属性时强制返回所有属性,即使属性值为 null。
  • -UseTransaction:在使用事务时,启用事务支持。
  • -ErrorAction:指定发生错误时的动作,默认为 Continue。
  • -WarningAction:指定发生警告时的动作,默认为 Continue。
  • -InformationAction:指定输出消息时的动作,默认为 Continue。
  • -ErrorVariable:指定发生错误时,将错误信息存储在变量中。
  • -WarningVariable:指定发生警告时,将警告信息存储在变量中。
  • -InformationVariable:指定输出消息时,将消息信息存储在变量中。
  • -OutVariable:将输出对象保存到变量中。
  • -OutBuffer:指定输出缓冲区大小。
示例

以下示例演示了如何使用 Get-ItemProperty 和 Select-Object 来获取 windows 注册表项属性:

# 获取注册表项属性
Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion' | Select-Object DisplayName, ReleaseId, EditionId

代码执行后,将会输出注册表项 HKLM:\Software\Microsoft\Windows NT\CurrentVersionDisplayNameReleaseIdEditionId 属性值,如下所示:

DisplayName                                ReleaseId                                                 EditionId
-----------                                ---------                                                 --------
Windows 10 Enterprise                      2009                                                     Enterprise
结论

Get-ItemProperty 和 Select-Object 是 PowerShell 中非常实用的数据处理工具,使用它们可以轻松地获取和操作系统对象属性。在编写 PowerShell 脚本时,应该多尝试使用这些命令,以提高脚本的效率和可读性。