Break


The Break statement very simply terminates just about any logic construct we've covered in this chapter including For, ForEach, While, and Switch. When a Break statement is encountered, PowerShell exits the loop and runs the next command in the command block or script:

 PS C:\> $i=0 PS C:\> $var=10,20,30,40 PS C:\> foreach ($val in $var) >> { >> $i++ >> if ($val -eq 30){break} >> } >> write-host "found a match at item $i" found a match at item 3 PS C:\> 

In this example we're searching an array of numbers for 30. When it is found, we want to stop looking, exit the ForEach loop, and display the message. Even though the Switch statement is not a loop, break also is used within a code block to force an exit from the entire Switch statement.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net