1

I have a basic PowerShell script that prompts for a partial string, searches (recursively) through the current folder and returns a list of files. It then prompts to ask what file the user wants to launch and opens the item when selected.

What's odd is that the output doesn't appear on the screen until after the read-host for item selection.

Here's an example:

$name = read-host "name?" # The search string

# Grab everything that matches the partial string
$searchlist = dir -recurse -filter "*$name*" | select FullName

# Create an empty array
$searchtable = @()
$number = 0

# Assign a number for each file to ease selection
$searchlist | foreach {
    $number++
    $o = [PSCustomObject]@{
        Number = $number
        Name = $_.FullName
        }
    $searchtable += $o
}

# Ensure the returned list is sorted by number
$sorted = $searchtable | sort Number

# Attempting to return the list here doesn't actually do anything
# The list is not displayed until after entering an item to the read-host
$sorted

$selectedvid = read-host "which one?"

$vid = $searchtable | where {$_.number -eq $selectedvid} | select -ExpandProperty Name
invoke-item -literalpath $vid

Working around this is reasonably trivial (the following works):

$name = read-host "name?"

$searchlist = dir -recurse -filter "*$name*" | select FullName

$searchtable = @()
$number = 0

$searchlist | foreach {
    $number++
    $o = [PSCustomObject]@{
        Number = $number
        Name = $_.FullName
        }
    $searchtable += $o
}

$sorted = $searchtable | sort Number
write-host "Number Name"
write-host "------ -----------------"
$sorted | foreach {write-host $($_.Number.ToString().PadRight(6,' ')) $_.Name}

$selectedvid = read-host "which one?"

$vid = $searchtable | where {$_.number -eq $selectedvid} | select -ExpandProperty Name
invoke-item -literalpath $vid

I just cannot figure out why the output isn't dumped to screen prior to entering a value into the $selectedVid variable.

Running PowerShell 5.1 on Windows 11.

Nic
  • 790
  • 1
  • 6
  • 16
  • 2
    Try adding `Out-Host` -> `$sorted | foreach {write-host $($_.Number.ToString().PadRight(6,' ')) $_.Name} | Out-Host` – Santiago Squarzon Sep 08 '22 at 01:32
  • Good to see you have a workaround. Just curious though. Why do this at all, when you can send the look-up to ***Out-Gridview***. The user can then use their mouse to select one or more files to open. For Example --- ***(Get-ChildItem -Path 'D:\temp').FullName -match $(read-host -Prompt 'Enter a name or partial on') | Out-GridView -Title 'Select a file or files using CRTL+Click' -PassThru | ForEach-Object {Invoke-Item $PSItem}*** – postanote Sep 08 '22 at 01:49
  • 1
    The lack of synchronization between pipeline output and to-host output (as well as other output streams) is limited to PS v5+ and a very specific - albeit still common - scenario: implicitly _table_-formatted output for types that do _not_ have formatting data defined for them. The - suboptimal - workaround is to force the pipeline output synchronously to the host (display) with `Out-Host`, as Santiago notes. See the linked duplicate for details. – mklement0 Sep 08 '22 at 01:52
  • @postanote I did not want to use a grid view for this and keep it all within the same window – Nic Sep 08 '22 at 15:11

0 Answers0