The Continue statement is essentially the opposite of Break. When the Break statement is encountered, PowerShell returns immediately to the beginning of a loop like For, ForEach, and While. You can also use Continue with Switch.
Here's a script that doesn't use Continue:
SwitchNoContinue.ps1
#SwitchNoContinue.ps1 $var="PowerShell123","PowerShell","123","PowerShell 123" Switch -regex ($var) { "\w" {write-host $_" matches \w"} "\d" {write-host $_" matches \d"} "\s" {write-host $_" matches \s"} Default {write-host "No match found for"$_ } }
When this script is run, all matching code blocks are run since there are multiple possible matches:
PowerShell123 matches \w PowerShell123 matches \d PowerShell matches \w 123 matches \w 123 matches \d PowerShell 123 matches \w PowerShell 123 matches \d PowerShell 123 matches \s PS C:\>
If we want the switch statement to only run code after the first match, then we can use Continue, which will keep processing each element in $var:
SwitchContinue.ps1
#SwitchContinue.ps1 $var="PowerShell123","PowerShell","123","PowerShell 123" Switch -regex ($var) { "\w" {write-host $_" matches \w" ; continue} "\d" {write-host $_" matches \d" ; continue} "\s" {write-host $_" matches \s" ; continue} Default {write-host "No match found for"$_ ; } }
This is the same script except with the addition of Continue. When run, the script produces the following output:
PowerShell123 matches \w PowerShell matches \w 123 matches \w PowerShell 123 matches \w PS C:\ >
Because each element of $var matches the \w regular expression, only the block of code associated with that part of the Switch statement is executed.