EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Continue
Secondary Sidebar
PowerShell Tutorial
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Continue

By Chirag NagarekarChirag Nagarekar

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.

Syntax:

Continue

Continue keyword is used as the syntax of the statement. This statement mostly used inside the condition.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Code:

$i = 0
while($i -lt 5){
$i++
if($i -eq 3){Continue}
Write-Host "i = $i"
}

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,650 ratings)

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more