'Powershell menu option

Hello I am trying to create a powershell menu that has options within it to run at a press of an option. However I am stuck where if I run the ping option the response comes in for a brief half a second and not sure why it disappears right away. Any advice or help appreciated! Im new to powershell still trying to get the hang of it using examples and things researched. If any learning advice im open ears as well!

Function Menu 
{
    Clear-Host        
    Do
    {
        Clear-Host                                                                       
        Write-Host -Object 'Please choose an option'
        Write-Host     -Object '**********************'
        Write-Host -Object 'Scripts to run' -ForegroundColor Yellow
        Write-Host     -Object '**********************'
        Write-Host -Object '1.  Ping PC '
        Write-Host -Object ""
        Write-Host -Object '2.  Restart ELP Desktops '
        Write-Host -Object ''
        Write-Host -Object '3.  Restart NOVI Desktops '
        Write-Host -Object ''
        Write-Host -Object 'Q.  Quit'
        Write-Host -Object $errout
        $Menu = Read-Host -Prompt '(0-3 or Q to Quit)'


Solution 1:[1]

You are shooting yourself in the foot by trying to show results without any form of pause and jumping backup to the Clear-Host command that wipes the screen.

In my experiment, which worked, I added the following function that I think I found from here:

function Pause {
    Write-Host -NoNewLine 'Press any key to continue...';
    $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}

Then, at the end of the code for option 1, I added:

Pause

This allowed me to see the results before Clear-Host wipes it.

FYI:

I noticed that Format-Table has a -Property switch the same as Select-Object. Did an experiment and found it looks like you can drop the last pipe and replace Select-Object with Format-Table and get the same results. I couldn't see a different, so give it a try.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Darin