Using the Shell.Application
object you can get this value from the file, but unfortunately, the property names are all Localized, so if you are looking for a property named Media created
on a non-english machine, you will not find it..
For .MOV files, the index of this property in numbered 4.
You can use below function to get that value for all the files in the path and then use that to rename them:
function Get-MetaData {
[CmdletBinding()]
[OutputType([Psobject[]])]
Param (
# Path can be the path to a folder or the full path and filename of a single file
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[string]$Path,
# Filter is unused if Path is pointing to a single file
[Alias('Pattern')]
[string]$Filter = '*.*',
[Alias('Indices')]
[int[]]$Properties = 1..500,
# Recurse is unused if Path is pointing to a single file
[switch]$Recurse,
[switch]$IncludeEmptyProperties
)
$item = Get-Item -Path $Path -ErrorAction SilentlyContinue
if (!$item) { Write-Error "$Path could not be found."; return }
if (!$item.PSIsContainer) {
# it's a file
$files = @($item)
$Path = $item.DirectoryName
}
else {
# it's a folder
$files = Get-ChildItem -Path $Path -Filter $Filter -File -Recurse:$Recurse
}
$shell = New-Object -ComObject "Shell.Application"
$objDir = $shell.NameSpace($Path)
foreach($file in $files) {
$objFile = $objDir.ParseName($file.Name)
$mediaFile = $objDir.Items()
foreach($index in $Properties) {
$name = $objDir.GetDetailsOf($mediaFile, $index)
if (![string]::IsNullOrWhiteSpace($name)) {
# to be on the safe side, remove any control character (ASCII < 32) that may be in there
$value = $objDir.GetDetailsOf($objFile, $index) -replace '[\x00-\x1F]+'
if (![string]::IsNullOrWhiteSpace($value) -or $IncludeEmptyProperties) {
[PsCustomObject]@{
Path = $file.FullName
Index = $index
Property = $name
Value = $value
}
}
}
}
}
# clean-up Com objects
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objFile)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objDir)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
# property index 3 --> Last Modified
# property index 4 --> Date Created # we'll use this one
# property index 5 --> Last Accessed
$SourceFolder = 'X:\Somewhere'
Get-MetaData -Path $SourceFolder -Filter '*.mov' -Properties 4 |
ForEach-Object {
# HH for 24-hour clock; hh for 12-hour clock
$dateCreated = '{0:yyyy-MM-dd HH.mm.ss ddd}' -f ([datetime]$_.Value)
$directory = [System.IO.Path]::GetDirectoryName($_.Path)
$extension = [System.IO.Path]::GetExtension($_.Path)
# test if the suggested new name is already in use, append a sequence
# number to the basename until we have a unique name
$newName = '{0}{1}' -f $dateCreated, $extension
$index = 1
while (Test-Path -Path (Join-Path -Path $directory -ChildPath $newName) -PathType Leaf) {
$newName = '{0}({1}){2}' -f $dateCreated, $index++, $extension
}
Rename-Item -Path $_.Path -NewName $newName -WhatIf
}
The -WhatIf
is a safety measure. It will make the code only show in the console what would happen. No file is actually renamed.
If you are convinced that output is correct, then remove the -WhatIf
switch and run the code again