Using PowerShell, how do I extract the Media Created date attribute value shown here in file explorer?
If possible I'd like to do this for all files in a given directory too.
3 Answers
Per the guidance on the enumerate file properties in PowerShell post, I've created a variation of this PowerShell logic and put it into a couple scripts to assist with the task.
I've included looping logic, some parsing and conditional logic, and logic that sets the variable data types to assist with getting the final desired value.
Note: I've put in conditional logic to help omit null values from the output.
PowerShell
Set $flPathto the full path and file name of a file. Set $attrName to the attribute name of the metadata property to get its value (e.g. the datetime of "Media Created").
$flPath = "M:\Fortune500\Millionaires Club.mov";
$attrName = "media created"
$path = $flPath;
$shell = New-Object -COMObject Shell.Application;
$folder = Split-Path $path;
$file = Split-Path $path -Leaf;
$shellfolder = $shell.Namespace($folder);
$shellfile = $shellfolder.ParseName($file);
$a = 0..500 | % { Process { $x = '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_); If ( $x.split("=")[1].Trim() ) { $x } } };
[int]$num = $a | % { Process { If ($_ -like "*$attrName*") { $_.Split("=")[0].trim() } } };
$mCreated = $shellfolder.GetDetailsOf($shellfile, $num);
$mCreated;Output Example
12/31/2020 7:55 PMPowerShell (all files in a directory)
Set $fldPath to the directory the files are in, set $flExt to the dot extension of the file types to search, and set $attrName to the attribute name of the metadata property to get its value.
$fldPath = "M:\Fortune500";
$flExt = ".mov";
$attrName = "media created"
(Get-ChildItem -Path "$fldPath\*" -Include "*$flExt").FullName | % { $path = $_ $shell = New-Object -COMObject Shell.Application; $folder = Split-Path $path; $file = Split-Path $path -Leaf; $shellfolder = $shell.Namespace($folder); $shellfile = $shellfolder.ParseName($file); $a = 0..500 | % { Process { $x = '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_); If ( $x.split("=")[1].Trim() ) { $x } } }; [int]$num = $a | % { Process { If ($_ -like "*$attrName*") { $_.Split("=")[0].trim() } } }; $mCreated = $shellfolder.GetDetailsOf($shellfile, $num); $mCreated;
};Supporting Resources
- Folder.GetDetailsOf method
- ForEach-Object
Standard Aliases for Foreach-Object: the '
%' symbol, ForEach - If()
- Split()
- Trim()
Here is some (untested) example code:
$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.Media.Duration")References:
2Works a treat and much simpler than the other answer.
To get the creation date, use:
Write-Host $ShellFile.ExtendedProperty("System.Media.**DateEncoded**")...rather than System.Media.Duration as shown in the example.