10

I have seen bits of this in other questions, but I am looking for a generic way to write a function that will take a file, and list its properties in such a way that they can be used. I am aware of the function called Get-ItemProperty but it does not list the properties that I am looking for (for example, given a .avi file, it will not tell me the length, frame width, etc).

Am I using the function wrong (all I am doing is: Get-ItemProperty file) or do I have to do this a different way?

I want to be able to say something like $a += $file.Length, or something like that for arbitrary properties.

soandos
  • 4,978
  • 13
  • 62
  • 96

2 Answers2

21

Sounds like you are looking for extended file attributes. These are not stored in System.IO.FileInfo.

One way is to use the Shell.Application COM object. Here is some example code:

http://web.archive.org/web/20160201231836/http://powershell.com/cs/blogs/tobias/archive/2011/01/07/organizing-videos-and-music.aspx

Say you had a video file: C:\video.wmv

$path = 'C:\video.wmv'
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)

You'll need to know what the ID of the extended attribute is. This will show you all of the ID's:

0..287 | Foreach-Object { '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_) }

Once you find the one you want you can access it like this:

$shellfolder.GetDetailsOf($shellfile, 216)
Leigh
  • 28,765
  • 10
  • 55
  • 103
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • 2
    `..` is an operator. It creates a loop control. So `0..287` says loop from `0` to `287`. So in the ForEach-Object block (`%` is the alias for it), the current object variable `$_` will be 0 then 1 then 2... until it's 287. – Andy Arismendi Feb 23 '12 at 20:04
  • I have checked all these attributes.. but I can't found last modified user name.. Is there any attribute or way to find it? Thank You in advance. – SID Nov 29 '17 at 09:54
  • 2
    there can more than 288 in some cases. You can run 0..500 for example to check how many attributes are there for a particular file type. For example, mp4 seems to have attributes till 334. – Ahmad Ismail Apr 24 '18 at 08:04
  • For me it was id 208. The format has weird space in it.. trying to cast it as datetime... if somebody has the solution ? Windows 10 / French – Jeff Dec 02 '21 at 12:47
  • try this to find out the id number #for($i=0;$i -le 500;$i++){ # $mCreated = $shellfolder.GetDetailsOf($shellfile, $i); # if($mCreated){ # Write-Host "$i + $mCreated" # } #} – Jeff Dec 02 '21 at 12:47
  • @Jeff Try `$DateTimeString -Replace '[^\w\- :]'`. That should remove everything that isn't a number, letter, dash, space, or colon. – Yay295 May 22 '22 at 21:34
  • @AndyArismendi thanks to you & the link, finally found the info I've searched for endlessly. all the googling in the world and linking to microsoft urls with dead links led me into a rabbit hole. bookmarking this ? and will create some powershell script(s) in my [github](https://github.com/bob-martin-ntx?tab=repositories) by EOM January 2023. – R. Martin Dec 23 '22 at 18:01
4

Another possible method which also uses the Shell.Application COM object but does not require you to know what the ID’s of the extended attributes are. This method is preferred over using the ID’s because ID’s are different in different versions of Window (XP, Vista, 10, etc.)

$FilePath = 'C:\Videos\Test.mp4'
$Folder = Split-Path -Parent -Path $FilePath
$File = Split-Path -Leaf -Path $FilePath
$Shell = New-Object -COMObject Shell.Application
$ShellFolder = $Shell.NameSpace($Folder)
$ShellFile = $ShellFolder.ParseName($File)

Write-Host $ShellFile.ExtendedProperty("System.Title")
Write-Host $ShellFile.ExtendedProperty("System.Media.Duration")
Write-Host $ShellFile.ExtendedProperty("System.Video.FrameWidth")
Write-Host $ShellFile.ExtendedProperty("System.Video.FrameHeight")

The code will display the title of the video (if it is set), duration (100ns units, not milliseconds), and the videos frame width and height.

The names of other extended properties can be found in the file propkey.h, which is part of the Windows SDK.

Additional information:

ShellFolderItem.ExtendedProperty method

Predefined Property Set Format Identifiers

Safwan
  • 181
  • 5