Home > Software engineering >  How to properly format the output Get-ItemProperty of multiple files?
How to properly format the output Get-ItemProperty of multiple files?

Time:12-18

I'm trying to get some detailed info about DLL files, using Get-ItemProperty. However I am not able to format the resulting output in a nice way.

This is what I got:

$zz = (ls C:\Windows\System32\*.dll).FullName

foreach ($f in $zz){
    Get-ItemProperty -Path $f |Select -ExpandProperty VersionInfo |select ProductVersionRaw, OriginalFilename, FileDescription | ft -HideTableHeaders
}

# ...
# 10.0.19041.867    vidreszr.dll     Windows Media Resizer

The result is ugly as it:

  • is not aligned,
  • have 2 newlines between each item,
  • are missing some filenames

Q: How can I format the resulting table properly?


UPDATE:

I made some progress with:

& { foreach ($f in $zz){ Get-ItemProperty -Path $f |Select -ExpandProperty VersionInfo |select ProductVersionRaw, OriginalFilename, FileDescription } } | ft

But still missing filename info on some files.


SOLUTION:

Thanks @n0rd's solution and some searching, I managed to get what I want:

$zz = (ls C:\Windows\System32\*.dll).FullName

& { foreach ($f in $zz){ Get-ItemProperty -Path $f |Select Name, @{Name="CreationTime";Expression= {"{0:yyyy}-{0:MM}-{0:dd}" -f ([DateTime]$_.CreationTime)}} -ExpandProperty VersionInfo |select ProductVersionRaw, CreationTime, Name, FileDescription } } | sort -Property Name | ft

This:

  • Changes the CreationTime format to: yyyy-MM-dd
  • Sorts on the real filename (Name)
  • To select only a specific ProductVersionRaw, insert:
    | ?{ $_.ProductVersionRaw -eq "10.0.19041.nnnn" } before | ft.

CodePudding user response:

Do it in a single command, there is no need to save the list and iterate over it:

Get-ChildItem c:\windows\system32\*.dll | Get-ItemProperty | ForEach-Object {$_.VersionInfo} | Select-Object -Property ProductVersionRaw,OriginalFilename,FileDescription

Get-ItemProperty can accept input from a pipe.

Also, using ForEach-Object over Select-Object -ExpandProperty is how I'd usually do it, but both work fine.

Alternatively, you could save the output of your commands up to ft call into a list, then feed that whole list into ft (so it has full context for formatting).

  • Related