0

Here is my script:

Get-WmiObject -Class win32_LoadOrderGroupServiceMembers |
ForEach-Object {
    New-Object -TypeName psobject -Property `
        @{
        "GroupOrder"=([wmi]$_.GroupComponent).GroupOrder
        "GroupName"=([wmi]$_.GroupComponent).Name
        "ServiceName"=([wmi]$_.PartComponent).Name
        "Started"=([wmi]$_.PartComponent).Started
        }
} | 
Where-Object { $_.started } | Sort-Object -Property grouporder -Descending

Read-Host -Prompt "Any key to exit:"

The problem is that when I execute it (double click on file), it halts on Read-Host -Prompt, but when I press enter it prints the results very briefly and then the window automatically closes.

How can I get the results of Get-WmiObject and Where-Object to print onscreen before the Read-Host -Prompt "Any key to exit:"?

I am somewhat new to Powershell scripting so go easy on me.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
fmotion1
  • 237
  • 4
  • 12
  • Start an interactive Powershell session instead of double-clicking the file (which shouldn't execute it anyway.) Press the Start button, type `powershell` and press enter. – vonPryz Oct 12 '21 at 06:10
  • Yes, that works. But I want to be able to double click it as well and halt execution after displaying the results for convenience. Either way I found a fix. I just had to pipe it into Write-Host at the very end. – fmotion1 Oct 12 '21 at 06:14

1 Answers1

3

Got the command to force the synchronous output from the article below. Using the Out-Host will do the trick. I placed your output within a variable so I can call it and use the cmdlet to display it before the prompt.

How do I prevent Powershell from closing after completion of a script?

Updated code:

$displayOutput = Get-WmiObject -Class win32_LoadOrderGroupServiceMembers |
ForEach-Object {
    New-Object -TypeName psobject -Property `
        @{
        "GroupOrder"=([wmi]$_.GroupComponent).GroupOrder
        "GroupName"=([wmi]$_.GroupComponent).Name
        "ServiceName"=([wmi]$_.PartComponent).Name
        "Started"=([wmi]$_.PartComponent).Started
        }
} | 
Where-Object { $_.started } | Sort-Object -Property grouporder -Descending

$displayOutput | Out-Host

Read-Host -Prompt "Any key to exit:"
Kelv.Gonzales
  • 558
  • 4
  • 13