Getting original object after piping via Out-GridView

Viewed 159

I need to show a list of file names without paths and open the selected file.

I can get it to work with full paths:

Get-ChildItem *.txt -Recurse | Sort-Object Name| Out-GridView -PassThru | Invoke-Item

But when I try to show only the file names it fails:

Get-ChildItem *.txt -Recurse | Sort-Object Name| Select-Object Name | Out-GridView -PassThru | Invoke-Item

By piping it through Get-Member I understand that Select-Object Name striped all non-Name properties. So how can I trace the original file object from what I got from GridView?

2 Answers

The problem is, that the Invoke-Item needs the path and not only the filename. You could store the get-childitem in a temporary variable:

$tmp = Get-ChildItem *.txt -Recurse | Sort-Object Name
$tmp | Select-Object Name | Out-GridView -PassThru
$tmp | Invoke-Item

Is that what you wanted? Please let me know if it worked, and if it did please mark my post as the answer. :)

You might want to use the DefaultDisplayPropertySet property of the hidden PSStandardMembers set for this:

$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]@('Name'))
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)

Get-ChildItem *.txt -Recurse | Select-Object * |
ForEach-Object {
    $_ | Add-Member MemberSet PSStandardMembers $PSStandardMembers; $_
} | Out-Gridview -PassThru | Select-Object FullName
  • Get-ChildItem *.txt has a default display propery set of: LastWriteTime, Length, Name
  • Select-Object * strips off the complete property set (displays all properties)
  • Add-Member MemberSet PSStandardMembers $PSStandardMembers adds a new display set with just the Name property and keeps the rest of the properties hidden
  • Select-Object FullName reveals the hidden FullName property
Related