Looping an Entire PowerShell Script

Is it possible to get a PowerShell Script to keep looping and repeating the same code indefinitely until terminated with something like a Control - C Keystroke?

I was thinking of something like a Do While Loop but need a little direction here with, hopefully some sample code as well.

Thank you all so much for the wonderful responses thus far. God bless you all!!!

You’re thinking along the right lines. At its most basic it would be:

while ($true) {

    #Loop forever

}

Thank you for the reply Matt.

May I ask what the code would look like beyond its most basic? Samples please?

 

Thank you and God bless you Sir!!!

Well it’s really just a case of replacing the #Loop forever comment with your own code. How about a talking clock?

Add-Type -AssemblyName System.speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer

while ($true) {

    $time = Get-Date -Format 'HH:mm:ss'
    $speak.Speak("The time is $time")

}

 

Sweet Matt!!!

I just tested it.

Its perfect. I think I will only need the core component but your example definitely illustrates the point clearly.

Thank you Sir and have a blessed day!!!

Let me ask you this Matt:

How would you get that particular script you wrote to go off at the quarter mark, (15, 30, 45, top of the hour), of every hour in the day continuously?

For a requirement like that, I would use a scheduled task.

My thoughts exactly. Thank you