- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use PowerShell break statement in foreach loop?
You can use the PowerShell Break statement with the foreach loop as mentioned below.
Example
foreach($obj in (Get-ChildItem D:\Temp)){ Write-Output $obj if($obj.Name -eq "cars.xml"){Break} }
Output
Directory: D:\Temp Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 13-12-2019 09:52GPO_backup d----- 24-11-2018 11:31 LGPO -a---- 27-01-2020 22:21 13962 Alias1 -a---- 26-01-2020 19:20 13818 aliases.txt -a---- 07-05-2018 23:00301 cars.xml
In the above example, when the file name matches the cars.xml then the loop gets terminated.
- Related Articles
- How to use PowerShell Break statement with the While Loop?
- How to use PowerShell break statement with the For loop?
- How to use PSCustomObject in PowerShell foreach parallel loop?
- How to use the foreach loop parallelly in PowerShell?
- How to use PowerShell Break statement with the Switch command?
- How do we use a break statement in while loop in C#?
- How to use the break statement to come out of a loop in JavaScript?
- How do we use foreach statement to loop through the elements of an array in C#?
- What is PowerShell Break statement?
- How to use ForEach-Object Parallel cmdlet in PowerShell?
- How to use PowerShell Break with the Label.
- How to break forEach() method in Lodash?
- How to use continue statement in Python loop?
- How to break a loop in JavaScript?
- foreach Loop in C#

Advertisements