如何为Out-GridView列添加自定义对象?附远程获取安装程序代码
Nice work on the initial script to pull installed apps from both 32-bit and 64-bit registry paths! Adding custom columns to your Out-GridView output is straightforward using calculated properties with Select-Object—this lets you inject new, meaningful columns alongside the existing app data.
Step 1: Add an Architecture Column (Distinguish 32/64-bit Apps)
Since you’re querying two separate registry paths, adding an Architecture column will help you quickly identify which apps are 32-bit vs 64-bit. Here’s the modified code:
$installed_apps = Invoke-Command -computername P1184CDC -scriptblock { # Fetch 64-bit apps and add Architecture column Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object DisplayName -ne $null | Select-Object *, @{Name='Architecture'; Expression={'64-bit'}} # Fetch 32-bit apps and add Architecture column Get-ItemProperty "HKLM:\Software\wow6432node\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object DisplayName -ne $null | Select-Object *, @{Name='Architecture'; Expression={'32-bit'}} } $installed_apps | Out-GridView -Wait
The @{Name='Architecture'; Expression={'64-bit'}} syntax creates a calculated column:
Namesets the column header in Out-GridViewExpressiondefines the value for each row (here, hardcoding "64-bit" or "32-bit" based on the registry path)
Step 2: Add Additional Custom Columns (e.g., Formatted Install Date)
If you want to clean up raw data like the default InstallDate (a string like 20231005), you can add a formatted date column. Here’s how:
$installed_apps = Invoke-Command -computername P1184CDC -scriptblock { # 64-bit apps with Architecture and Formatted Install Date Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object DisplayName -ne $null | Select-Object *, @{Name='Architecture'; Expression={'64-bit'}}, @{Name='InstallDateFormatted'; Expression={ if ($_.InstallDate) { [DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null).ToString('MM/dd/yyyy') } else { 'N/A' } }} # 32-bit apps with Architecture and Formatted Install Date Get-ItemProperty "HKLM:\Software\wow6432node\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object DisplayName -ne $null | Select-Object *, @{Name='Architecture'; Expression={'32-bit'}}, @{Name='InstallDateFormatted'; Expression={ if ($_.InstallDate) { [DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null).ToString('MM/dd/yyyy') } else { 'N/A' } }} } $installed_apps | Out-GridView -Wait
Step 3: Limit Columns for Clarity
If you don’t need all the default properties returned by Get-ItemProperty, replace * with a list of specific properties to keep your Out-GridView focused:
Select-Object DisplayName, DisplayVersion, Publisher, @{Name='Architecture'; Expression={'64-bit'}}, @{Name='InstallDateFormatted'; Expression={...}}
Feel free to adjust the calculated properties to match any custom columns you need—whether it’s a status flag, a parsed version number, or something else!
内容的提问来源于stack exchange,提问作者Rakha

