Advertisements

adam

Privacy & Cookies

This site uses cookies. By continuing, you agree to their use. Learn more, including how to control cookies.

You ’ ve developed a PowerShell handwriting that returns some utilitarian information to the exploiter. At the end of the script execution, you want the user to “ Press any key to continue… ” before exiting. How to do it ?

Solution 1: For PowerShell Console

Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

If you run the handwriting above in Windows PowerShell commandline console, you will get the follow results when you press the Enter key :

VirtualKeyCode  Character  ControlKeyState  KeyDown
--------------  ---------  ---------------  -------
            13  ...        0                True

however, if you are running your script in PowerShell ISE, you will receive the succeed error :
Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
To resolve this mistake in PowerShell ISE, see the next solution.

Solution 2: Works in PowerShell ISE

here is a simple way to pause the script execution and delay for the drug user to press the ENTER key to continue. This works for both the PowerShell commandline console american samoa well as in the PowerShell ISE .

read-host “Press ENTER to continue...”

Solution 3: MessageBox UI

Another way to pause the script execution and wait for the exploiter ’ mho interaction is by showing a MessageBox UI. This works for both the PowerShell commandline console table equally well as in the PowerShell ISE .

$Shell = New-Object -ComObject "WScript.Shell"
$Button = $Shell.Popup("Click OK to continue.", 0, "Hello", 0)

This will result in a MessageBox UI as follows :
messagebox

Solution 4: Pause function

here is an encompassing solution that works whether you are running your script in the PowerShell commandline console table or in the PowerShell ISE :

Function Pause ($Message = "Press any key to continue...") {
   # Check if running in PowerShell ISE
   If ($psISE) {
      # "ReadKey" not supported in PowerShell ISE.
      # Show MessageBox UI
      $Shell = New-Object -ComObject "WScript.Shell"
      $Button = $Shell.Popup("Click OK to continue.", 0, "Hello", 0)
      Return
   }

   $Ignore =
      16,  # Shift (left or right)
      17,  # Ctrl (left or right)
      18,  # Alt (left or right)
      20,  # Caps lock
      91,  # Windows key (left)
      92,  # Windows key (right)
      93,  # Menu key
      144, # Num lock
      145, # Scroll lock
      166, # Back
      167, # Forward
      168, # Refresh
      169, # Stop
      170, # Search
      171, # Favorites
      172, # Start/Home
      173, # Mute
      174, # Volume Down
      175, # Volume Up
      176, # Next Track
      177, # Previous Track
      178, # Stop Media
      179, # Play
      180, # Mail
      181, # Select Media
      182, # Application 1
      183  # Application 2

   Write-Host -NoNewline $Message
   While ($KeyInfo.VirtualKeyCode -Eq $Null -Or $Ignore -Contains $KeyInfo.VirtualKeyCode) {
      $KeyInfo = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
   }
}

You will see Press any key to continue... if you are running in the PowerShell commandline console table, or a MessageBox UI if you are running in PowerShell ISE .
Have fun with PowerShell !

Advertisements

Share this:

Leave a Reply

Your email address will not be published.