How to use PowerShell Break statement with the Switch command?

PowerShellMicrosoft TechnologiesSoftware & Coding

In Switch command, when the single value passed and if it matches the condition then the loop gets automatically exited but when there are multiple values passed and if the value matches the first condition and if you want to terminate the loop then you can use the Break statement. An example is given below.


Switch (3,5){
   1 {"This is One"}
   2 {"This is Two"}
   3 {"This is Three"; Break}
   4 {"This is Four"}
   5 {"This is Five"; Break}


This is Three

You can notice in the above output that second parameter 5 won’t be executed because the 3 has already been executed with the break statement.

Updated on 12-Mar-2020 07:28:38