简体   繁体   中英

How to export file properties in csv using powershell

I wanted to export a csv-File with File-Properties from some tif-Files.

With this command

Get-ChildItem -Recurse C:\tifs\ | 
ForEach-Object {$_ | add-member -name "Owner" -membertype noteproperty `
-value (get-acl $_.fullname).owner -passthru} | Sort-Object fullname | 
Select FullName,CreationTime,LastWriteTime,Length,Dimensions | 
Export-Csv -Force -NoTypeInformation C:\Test\export.csv

I can export a csv just fine. But as soon as I want to add properties like vertical resolution it fails. I don't quite understand why.

In order to get to the "extended" file properties (like Dimension and Resolution metadata) you have to resort to using the Windows Visual Basic shell options from inside PowerShell as Steven helpfully pointed out. Here is a code sample that should give you the result:

$files = @()
$folder = (New-Object -ComObject Shell.Application).namespace("C:\tifs") 

# Loop through each file in folder
foreach ($f in $folder.Items()) {
  $a = 0

  # Print all the available properties (for debugging purposes)
  for ($a ; $a  -le 266; $a++) {  
    if($folder.GetDetailsOf($f, $a)) { 
      Write-Host "Property: $($folder.GetDetailsOf($folder.items, $a))"
      Write-Host "Value: $($folder.GetDetailsOf($f, $a))"
      Write-Host "Index: $($a)"
    }
  }

  # Store data in custom PowerShell object
  $obj = New-Object -TypeName PSOBJECT
  # Fill each property with the file metadata (by index number)
  $obj | Add-Member -MemberType NoteProperty -Name FullName -Value $folder.GetDetailsOf($f, 194)
  $obj | Add-Member -MemberType NoteProperty -Name CreationTime -Value $folder.GetDetailsOf($f, 4)
  $obj | Add-Member -MemberType NoteProperty -Name LastWriteTime -Value $folder.GetDetailsOf($f, 5)
  $obj | Add-Member -MemberType NoteProperty -Name Length -Value $folder.GetDetailsOf($f, 1)
  $obj | Add-Member -MemberType NoteProperty -Name Dimensions -Value $folder.GetDetailsOf($f, 31)
  # Add custom object to a collection
  $files += $obj
}

# Export collection to CSV
$files | Export-Csv -Force C:\Test\export.csv -NoTypeInformation -Encoding UTF8

From what I can tell there's no obvious PowerShell/.Net approach to getting additional file meta data. However. there are some COM based approaches.

  1. Check Scripting Guys
  2. And they reference this

You will still have to correlate the data. I usually do that by building hash table keyed of same values, in you can index the metadata using the path property, then use the FullName property of the file info objects to reference it, so you can the properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM