Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Control flow statements


Control flow statements in PowerShell are used to control the flow of execution of a script. They allow you to make decisions based on conditions, repeat blocks of code, and jump to different parts of a script based on specific conditions.

The most common control flow statements in PowerShell are:

  1. If statements - Used to execute code if a condition is true and skip the code if the condition is false.
  2. Switch statements - Used to execute code based on the value of a variable or expression.
  3. For loops - Used to execute a block of code a specific number of times.
  4. While loops - Used to execute a block of code as long as a condition is true.
  5. Do-While loops - Similar to While loops, but the code block is executed at least once before the condition is evaluated.
  6. ForEach-Object - Used to iterate over a collection of objects and perform a specific action on each object.

Table of contents