Using Flow Control Statements

Once scripting support is enabled on Windows PowerShell, you have access to some advanced flow control cmdlets. However, this does not mean you cannot do flow control inside the console. You can certainly use flow control statements inside the console. This is shown here:

PS C:\> Get-Process | foreach ( $_.name ) { if ( $_.name -eq "system" ) {
Write-Host "system process is ID : " $_.ID } }

The problem is the amount of typing. It may be preferable to save such a command in a script. Besides saving a long command in a file, there is also an advantage in readability. For example, you can line up the curly brackets and the other components of the commands. You can also avoid hard-coding process names into the script and ...

Get Windows PowerShell™ Scripting Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.