EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Continue
 

PowerShell Continue

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated February 28, 2023

PowerShell Continue

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

keyword

Explanation: In the above example, the variable $i value is first set to 0 initially and while loop iterates until $i value becomes 5 but when the value of the $i reaches to 3 it executes the Continue statement and the rest of the program is skipped and execution returns to the top of the loop. So in the output, you can see the value 3 is skipped and the rest of the values are displayed.

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:

For loop

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:

an array

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:

Rabbit

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:

PowerShell Continue - 5

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:

PowerShell Continue - 6

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:

Foreach loop

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:

PowerShell Continue - 8

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:

PowerShell Continue - 9

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:

PowerShell Continue - 10

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:

first loop

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 –

  1. PowerShell Get-Content
  2. Useful PowerShell Scripts
  3. PowerShell ForEach Object
  4. PowerShell Alias
  5. PowerShell Get-Service | Examples

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW