Add-member for Powershell V2

Thanks to this forum, I have a script to sort an array of fully qualified file paths by just the filename portion.

            $pattern = [regex]".+\\(.+)$"
            $files = $files | Add-Member @{BaseName=$null} -PassThru
            for ($i = 0; $i -lt $files.Count; $i++)
                $files[$i] -match $pattern | Out-Null
                $files[$i].basename = $Matches[1]
            $files | sort BaseName

However the Add-Member fails when run on a machine running Powershell V2 - error below - can someone help me to reformat this line to run successfully on a V2 box?

Very new to Powershell...and very grateful for all your help!

Cannot bind parameter 'MemberType'. Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "System.Management.Automation.PSMemberTypes". ()

  • OK, before we spend a lot of cycles going retro, why is it being run on a machine that doesn't have the current version?  Since you are running that against a file and not live against some underlying system dependency, it can be run anywhere.  If you run it on a V3 machine or later you're done.  If you upgrade the machine you're trying to run it on, you're done.  Is this an Exchange box, or are we dealing with some institutional mythology around upgrades or security?

  • Unfortunately the machine this will be run on is not one under my control - would be easier to push my head through a needle head than get it upgraded