2

I'm trying to do the following:

Display a list from a multidimensional array:

    $AssetList | Where-Object {$_.agenttype -eq "N"} | Select-Object number, hostname, agentstatus | Write-Output

Then read in a number from the user:

    $Victim = Read-Host -Prompt 'Enter the number of the host you want rid of...' 

What happens in reality is the Read-Host is displayed before the Write-Output. How can I flip it and reverse it?

Ben Bob
  • 23
  • 3
  • 1
    Possible duplicate of [Unable to Pause or Sleep after Select-Object](http://stackoverflow.com/questions/34835327/unable-to-pause-or-sleep-after-select-object) – user4003407 Oct 27 '16 at 10:00
  • My solution was to use `Out-String` instead of `Write-Output`. This solution doesn't seem to mention either... – Ben Bob Oct 27 '16 at 14:52

1 Answers1

0

I think the issue here is that you're using Write-Output (which writes to the output stream for your script/function). If you just want the data to be displayed on the console before the Read-Host cmdlet, try using Out-String | Write-Host. You can probably get away with just Out-String on its own.

Out-String

The Out-String cmdlet converts the objects that Windows PowerShell manages into an array of strings. By default, Out-String accumulates the strings and returns them as a single string, but you can use the stream parameter to direct Out-String to return one string at a time. This cmdlet lets you search and manipulate string output as you would in traditional shells when object manipulation is less convenient.

https://technet.microsoft.com/en-us/library/hh849952.aspx

Charlie Joynt
  • 4,411
  • 1
  • 24
  • 46