Continuing my post on Filtering files by their metadata (extended properties), and a question raised by Ankor in the comments section, I decided to quickly wrap up a new function (based on the one in the post) that simply adds the specified extended properties as new note properties to the object in the pipeline. As we would be reading only the required extended properties it would run much faster, and we could simply filter those objects later using the Where-Object (or with any other comparison statement).
The functions’ code is:
function Add-ExtendedAttribute {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[Alias(‘FullName’, ‘PSPath’)]
[string[]]$Path,
[Parameter(Mandatory = $true)]
[ValidateRange(0,287)]
[int[]]$ExtendedAttributeId
)
begin {
$oShell = New-Object -ComObject Shell.Application
}
process {
$Path | ForEach-Object {
if (Test-Path -Path $_ -PathType Leaf) {
$FileItem = Get-Item -Path $_
$oFolder = $oShell.Namespace($FileItem.DirectoryName)
$oItem = $oFolder.ParseName($FileItem.Name)
$ExtendedAttributeId | ForEach-Object {
$ExtPropName = $oFolder.GetDetailsOf($oFolder.Items, $_)
$ExtValName = $oFolder.GetDetailsOf($oItem, $_)
$params = @{
InputObject = $FileItem
MemberType = ‘NoteProperty’
Name = $ExtPropName
Value = $ExtValName
}
$FileItem = Add-Member @params -PassThru
}
}
$FileItem
}
}
end {
$oShell = $null
}
}
And you can use it:
# Rating: 19
dir -Path G:MusicaMSpecial | Add-ExtendedAttribute -ExtendedAttributeId 19 |
Where-Object { $_.Rating -eq ‘2 Stars’ } | Copy-Item -Destination G:Temp
# Genre: 16
dir -Path G:MusicaMSpecial | Add-ExtendedAttribute -ExtendedAttributeId 16 |
Where-Object { $_.Genre -eq ‘Rock’ } | Copy-Item -Destination G:Temp
Thank you Ankor for raising the question.
HTH,
Martin.