Adding a pause function to your PowerShell scripts can be useful for prompting users to press any key to continue. This guide will show you how to implement a simple pause function to incorporate user interaction within your scripts.
PowerShell Pause Function
To create a pause function in PowerShell, you can define a function that displays a message and waits for the user to press a key. Here is the function:
1 2 3 4 5 | function Pause ($Message="Press any key to continue") { Write-Host -NoNewLine $Message $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") Write-Host "" } |
This function includes the following components:
Write-Host -NoNewLine $Message
: Displays the message without adding a new line.$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
: Waits for the user to press any key without echoing the key press.Write-Host ""
: Adds a new line after the key press.
1 Comment
Neil · 25 January 2025 at 22h52
Handy little function, thanks for sharing