]> git.ardis.lu - ArdisUtilities/commitdiff
Add Get-ExtendedFileProperties
authorArdis Lu <ardisglu@gmail.com>
Sun, 26 Dec 2021 08:48:49 +0000 (00:48 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sun, 26 Dec 2021 08:48:49 +0000 (00:48 -0800)
ArdisUtilities/ArdisUtilities.psd1
ArdisUtilities/ArdisUtilities.psm1

index e2573c4f20aa7d6ce90f64a91fc6bdb59ca58ce9..37a1b70f78262edd9fdd23f69ae41b762cce48c7 100644 (file)
@@ -4,7 +4,7 @@
   GUID              = '57a6bea3-0ea5-44b3-95d2-71d122772f1c'
   Author            = 'Ardis Lu'
   Description       = 'My personal utility functions.'
-  FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio')
+  FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio', 'Get-ExtendedFileProperties')
   CmdletsToExport   = @()
   VariablesToExport = @()
   AliasesToExport   = @()
index 20cabea369aabf17114319c68927ee909039749f..aa24c5b0b40a298c126629aa7cf27cca91c00457 100644 (file)
@@ -304,3 +304,109 @@ function Get-SizeDurationRatio {
     }
   }
 }
+
+class ExtendedFileProperties {
+  [String]$Path
+  [String]$FileName
+  [String]$PropertyIndex
+  [String]$PropertyName
+  [String]$PropertyValue
+}
+
+$ExtendedFilePropertiesDisplay = @{
+  TypeName                  = 'ExtendedFileProperties'
+  DefaultDisplayPropertySet = 'FileName', 'PropertyName', 'PropertyValue'
+}
+Update-TypeData @ExtendedFilePropertiesDisplay -Force
+
+function Get-ExtendedFileProperties {
+  <#
+  .SYNOPSIS
+    Gets a file's extended file properties.
+
+  .DESCRIPTION
+    Creates a new Shell Folder object from the file's parent folder path. Then creates a FolderItem
+    object for the file. Finally, loops through all indices between 0 and 512 and returns all non-null
+    properties for that file.
+
+  .PARAMETER Path
+    The path to the file to get extended file properties for.
+
+  .EXAMPLE
+    PS> Get-ExtendedFileProperties example.mp4 
+
+    FileName    PropertyName      PropertyValue
+    --------    ------------      -------------
+    example.mp4 Name              example.mp4
+    example.mp4 Size              11.3 MB
+    example.mp4 Item type         MP4 Video File (VLC)
+    example.mp4 Date modified     12/25/2021 11:11 PM
+    example.mp4 Date created      12/26/2021 12:04 AM
+    example.mp4 Date accessed     12/26/2021 12:37 AM
+    example.mp4 Attributes        A
+    example.mp4 Perceived type    Video
+    
+    (...)
+
+  .EXAMPLE
+    PS> Get-ExtendedFileProperties example.mp4 | where PropertyName -like 'Date*'
+
+
+    FileName    PropertyName  PropertyValue
+    --------    ------------  -------------
+    example.mp4 Date modified 12/25/2021 11:11 PM
+    example.mp4 Date created  12/26/2021 12:04 AM
+    example.mp4 Date accessed 12/26/2021 12:37 AM
+
+  .EXAMPLE
+    PS> @('example.mp4', 'example2.mp4') | Get-ExtendedFileProperties | where PropertyName -in 'Size', 'Length', 'Frame width', 'Frame height', 'Total bitrate'
+
+
+    FileName     PropertyName  PropertyValue
+    --------     ------------  -------------
+    example.mp4  Size          11.3 MB
+    example.mp4  Length        00:01:27
+    example.mp4  Frame height  352
+    example.mp4  Frame width   624
+    example.mp4  Total bitrate 1091kbps
+    example2.mp4 Size          4.80 MB
+    example2.mp4 Length        00:00:41
+    example2.mp4 Frame height  720
+    example2.mp4 Frame width   1280
+    example2.mp4 Total bitrate 952kbps
+  #>
+
+  [OutputType([ExtendedFileProperties])]
+  [CmdletBinding()]
+  param(
+    [Parameter(Mandatory, ValueFromPipeline)]
+    [ValidateScript({ Test-Path -LiteralPath $_ }, ErrorMessage = "'{0}' is not a valid path.")]
+    [String]$Path
+  )
+
+  begin {
+    $shell = New-Object -ComObject Shell.Application
+  }
+
+  process {
+    $resolvedPath = Resolve-Path -LiteralPath $Path
+    $folderPath = Split-Path $resolvedPath
+    $fileName = Split-Path $resolvedPath -Leaf
+
+    $folderObject = $shell.NameSpace($folderPath)
+    $fileObject = $folderObject.ParseName($fileName)
+
+    foreach ($i in 0..512) {
+      $property = $folderObject.GetDetailsOf($fileObject, $i)
+      if ($property) {
+        [ExtendedFileProperties] @{
+          Path          = $resolvedPath
+          FileName      = $fileName
+          PropertyIndex = $i
+          PropertyName  = $folderObject.GetDetailsOf($null, $i)
+          PropertyValue = $property
+        }
+      }
+    }
+  }
+}
This page took 0.047737 seconds and 4 git commands to generate.