Updated February 28, 2023
Introduction to PowerShell Continue
Continue statement in PowerShell is used to skip the current execution of the loop and to return the execution to the top of the innermost loop. Continue statement is generally used with the conditional statement which is controlled by loops like While, For and Foreach. When the continue statement is executed inside the loop, it skips the current iteration and moves to the innermost loop for the next iteration.
Syntax:
Continue
Continue keyword is used as the syntax of the statement. This statement mostly used inside the condition.
Code:
$i = 0
while($i -lt 5){
$i++
if($i -eq 3){Continue}
Write-Host "i = $i"
}
Output:
Examples to Implement PowerShell Continue
Below are the examples to implement:
Example #1 – Continue Statement with For loop
Code:
for($i=1;$i -le 5; $i++){
if($i -eq 3){Continue}
Write-Host "i = $i"
}
Output:
Explanation: Similar to the While loop, when the for loop executes the Continuestatement, program loops through each value and when the value becomes 3 it skip the execution and returns to the For loop again.
Example #2 – Continue statement with an array
Code:
$array = 1..5
foreach($val in $array){
if($val -eq 3){Continue}
Write-Host "Val : $val"
}
Output:
In this example, we have declared an array from values 1 to 5 and when the value of $i becomes 3, continue statement is executed and that iteration is skipped.
Another Example of the continue statement using an Array. In the below example, we have string array and we need to skip the processing for the data if the value of the array is “Rabbit”. Check the code below to perform the same.
Code:
$values = "Dog","Cat","Rabbit","Tiger"
foreach($val in $values){
if($val -eq "Rabbit"){Continue}
$val
}
Output:
Example #3 – Continue statement with the Foreach loop
In the below example, we will check if the given file name called “style.css” matches and if yes then we will skip that step and continue for the next step. We have content of the folder as below and Style.cssfile is there in the folder content.
Code:
Get-ChildItem D:\Temp
Output:
Now, we will use the continue statement to skip the Style.cssoperation. To do so, we will check each file in the Get-ChildItem and if the file is found, the operation is skipped for that file.
Code:
$files = Get-ChildItem D:\Temp
foreach($file in $files){
if($file.Name -eq "Style.css"){Continue}
$file
}
Output:
You won’t see style.css file in the output.
Another example of Foreach loop.
In the below example, we need to get the disk information of the computer but we need only the fixed local disks on the computer. To do so we first need to check the parameter which provides the fixed local disks. We will use the below command for it.
Code:
Get-CimInstance Win32_LogicalDisk
Output:
We have the output of the logical disks on the local server and we need the DriveType ‘3’ for our output. We can use other methods like Where or Filter. We will try to achieve the same result that is produced by the Where pipeline command below.
Code:
Get-CimInstance Win32_LogicalDisk | where{$_.DriveType -eq '3'}
Output:
We are going to write a script to get the same output with the Continue Statement.
Code:
$disks = Get-CimInstance Win32_LogicalDisk
foreach($disk in $disks){
if($disk.Drivetype -ne '3'){Continue}
$disk
}
In the above script, we have stored all the disks into the variable called $disksand scan each disk one by one with its drivetype. If the DriveType is other than ‘3’ then processing for the disk is skipped so the $disk command won’t be executed at that time and when the DriveType is‘3’ then it skips the Continue statement and runs the $disk output which is the result of the Fixed logical disk.
Output:
Example #4 – Continue statement with a nested loop
In the below example, we will see how the Continue statement works with the nested loop.
Code:
for($i=1;$i -lt 5;$i++){
for($j=1;$j -lt 3;$j++){
if($i -eq 3){Continue}
Write-Host "i = $i"
Write-Host "j = $j`n"
}
}
Output:
In this example, we are using a continue statement inside the second for loop. So once the value of $i reaches 3, continue statement is executed and it skips executing the next two commands in the script. As you can see in the output, the value of $i(number 3) is excluded so the value associated with the variable $j.
If you move the continue statement in the first loop, the result will be the same.
Code:
for($i=1;$i -lt 5;$i++){
if($i -eq 3){continue}
for($j=1;$j -lt 3;$j++){
Write-Host "i = $i"
Write-Host "j = $j`n"
}
}
Output:
Conclusion
Continue statement seems quite useful when there are several values to be processed and when you want to skip the check for the particular values so the commands will not be processed for them. Please note that the Continue statement returns execution to the top of the loop and if you want the loop to be terminated entirely then you need to use the Break statement.
Recommended Articles
This is a guide to PowerShell Continue. Here we discuss an introduction, syntax with examples to implement with proper codes and outputs. You can also go through our other related articles to learn more –