Adding extended properties as NoteProperties


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:\Musica\MSpecial | Add-ExtendedAttribute -ExtendedAttributeId 19 |

    Where-Object { $_.Rating -eq ‘2 Stars’ } | Copy-Item -Destination G:\Temp

 

# Genre: 16

dir -Path G:\Musica\MSpecial | Add-ExtendedAttribute -ExtendedAttributeId 16 |

    Where-Object { $_.Genre -eq ‘Rock’ } | Copy-Item -Destination G:\Temp

 

Thank you Ankor for raising the question.

HTH,

\Martin.

Comments (3)

  1. ankor says:

    Oh wow that is an awesome modification and much faster and I’m getting filtered results as expected with the copy portion. Can’t thank you enough. This PowerShell stuff is incredible. Never paid attention to it before but flexibility lends itself to a lot. Again thanks.

  2. ankor says:

    I checked the link for extended property indexes and noticed it shows 20 for Genre and Rating isn’t listed. But you used 16 for Genre and it did work for me. You used 19 for Rating when the chart lists 19 for Track Number. Why the discrepancy? Thank you very much.

Skip to main content