Out-GridView -Passthru

hi , i m developing a tool to make a search on a network drive the results are diplayed on a out-gridview window but directory is not mentionned only the filename is displayed . i suppose the get childItem command doesn’t
$fileName –>name of the file requested

$Files = (Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue | Where-Object { ($.PSIsContainer -eq $false) -and ( $.FullName -like “$fileName”) }
#test if $files contains result
if ($Files.count -eq 0) {

write-host " no result containing expression:" $fileName

} else {
#here we select what information we want to be displayed in out-gridview and then Passthru
$Files | Select-Object Name,Directory | Out-GridView -Title “choisissez le fichier à ouvrir” -PassThru | ii
}

Nothing happens even if i select a file in the out-gridview window . i suppose there is a mismatch object type between
$Files | Select-Object Name,Directory and Out-GridView -Passthru ??

help please !!
the results a good until the window of out-gridview but the invoke item doesn’t open the file selected …

I think I see what you are trying to do with the

Select-Object Name, Directory
before the
Out-GridView
, you want it nice and pretty for the user :smiley:

You’re right though, that’s not going to work. As far as I can tell, this is because the Name and the Directory do not have the full path to the file.

A way that I have to work around this is

$Selected = $files | Select-Object -Property Name,Directory | Out-GridView -PassThru;

Invoke-Item -Path ($files | Where-Object {($_.Name -eq ($Selected.Name)) -and ($_.Directory.FullName -eq ($Selected.Directory.FullName))}).FullName;

Include FullName in your Select-Object property list, if you want to include the directory.

However, Invoke-Item can’t “invoke” a file with just the file name. Including FullName may help, but if it doesn’t, then you’re going to need to NOT use Select-Object, but instead let the entire file object appear in the GridView, so that the entire file object can be passed through.

Hi DOn Jones,
Thanks for your reply

Actually it works when i let the entire object pass through –>$Files | Out-GridView -Title “choose the file to open” -PassThru | ii
But in the Out-GridView window we can’t see the location of the directory even with FullName in the Select-Object property list

Anyway I ll let it this way if there is no choice …

You could probablydo it like this:

$Files | Select-Object FullName,Name,Directory | Out-GridView -PassThru | Invoke-Item -Path {$_.FullName}

or if you really want to avoid, showing the FullName:

$Files | Select-Object Name,Directory | Out-GridView -PassThru | Invoke-Item -Path{Join-Path $_.Directory $_.Name}

hello Shane O’Neill,

your solution is 50/100 effective as it works for all the files in a certain directory and doesn’t work at all for certain directory… and powershell doesn’t generate any error message …

it seems that out-GridView doesn’t like long path ex:Z:\level1\Telecommunication\Service client\Téléphonie mobile\Applications\file.doc
and for this path : Z:\level1\Application Métier\SIGNORDO (signature ordonnances) - AP0177\otherfile.doc
it works like a charm ???

hello dirk,

your solution doesn’t generate any error powershell accepts it but the choosen file doesn’t open ,it’s like invoke item doesn’t receive enough information …

ty for taking my request in consideration

Strange things, it works for me if I pipe the results of:

(dir -File)[0]

into it. Since Invoke-Item performs the default action on the specified item, I assume that you have the application to launch the file installed on the PC that you run this. I don’t think it’s something about the PowerShell version either, but just to be sure, which version of PowerShell are you using ($PSVersionTable will tell you)?

hi dirk,
the problem doesn’t come from wich software ii uses to open the requested file,
when i choose a location for a pdf file it works but il i choose the same file in a anoter directory on the same network drive nothings happens ???

Name Value


PSVersion 5.0.10586.117
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.10586.117
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

hi dudes,

i ve found out a solution to bypass my problem here is the code :
#here we select or specifie the file name
[string]$myfile = ‘Linux-clients.pdf’
#the result is a collection of objects containing the expression ‘$myfile’ and many other things
$search = (Get-ChildItem -Recurse -File -Force -Path E:\ -ErrorAction SilentlyContinue | Where-Object { $_.FullName -like “$myfile” } )

$search | Select-Object PSpath,FullName | Out-GridView -Passthru | ii

#This is the only way you can send the path and fullname to invoke-item via a select-object command !

thank you dude for orienting me in the right direction !!