Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

We Pause a Moment

Posted on January 17, 2011

Most of the time when running a PowerShell script or series of commands you want to blast your way through. But there might be times where you want to pause script execution. Perhaps to display an informational message or to simply pace execution. In my work as a trainer and speaker I often use the famous Start-Demo script to walk through demo scripts. But you may not have that function or it doesn't make sense to use in the context of your script. For those situations you can use my Set-Pause function.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

[cc lang="PowerShell"]
Function Set-Pause {
Param(
[Parameter(Position=0)]
[string]$Message="Press any key to continue...",

[ValidateSet("Black","DarkMagenta","DarkRed","DarkBlue",
"DarkGreen","DarkCyan","DarkYellow","Red",
"Blue","Green","Cyan","Magenta","Yellow",
"DarkGray","Gray","White" )]
[string]$ForegroundColor="Green",

[ValidateScript({$_ -gt 0})]
[Alias("seconds")]
[int32]$Sleep,

[Alias("cls")]
[switch]$Clear
)

Write-Host $Message -foregroundcolor $foregroundcolor

#pause for specified number of seconds
if ($Sleep)
{
Start-Sleep -Seconds $sleep
}
else
{
#wait for a key press
$Running=$true

While ($Running)
{
if ($host.ui.RawUi.KeyAvailable) {
$key = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
if ($key) {
$Running=$False
}
}
sleep -millisecond 100
} #end While
}

if ($Clear)
{
Clear-Host
}
} #end function
[/cc]
This function uses Write-Host to display a colored message to the console. I take advantage of the foregroundcolor so that the pause prompt, isn't co-mingled with your PowerShell output. You can use same parameter with my function to change the value. The default is Green.

The default pause behavior is to wait for the user to press any key. This is accomplished by checking the KeyAvailable property from the host's RawUI property.
[cc lang="PowerShell"]
if ($host.ui.RawUi.KeyAvailable) {
$key = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
if ($key) {
$Running=$False
}
}
[/cc]
As such, this won't work in the ISE and I can't vouch it will work with other PowerShell hosts. Although it will work with the alternative behavior using the -Sleep parameter. With this parameter you can specify a number of seconds to pause, after which time execution continues. The other option is to use -Clear, which will clear the screen after every prompt. Finally, the script file with the function also defines an alias, Pause.

Here's an example.

Download Set-Pause.ps1


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “We Pause a Moment”

  1. Pingback: Tweets that mention We Pause a Moment | The Lonely Administrator -- Topsy.com

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d