concept statement in category powershell

appears as: statements, statement, statement, statements
Windows PowerShell in Action, Third Edition

This is an excerpt from Manning's book Windows PowerShell in Action, Third Edition.

The final simplification is to get the value itself. We’ll introduce a new cmdlet that lets you do arbitrary processing on each object in a pipeline. The ForEach-Object cmdlet executes a block of statements for each object in the pipeline. You can get an arbitrary property out of an object and then do arbitrary processing on that information using the ForEach-Object command. Here’s an example that adds up the lengths of all the objects in a directory:

Figure 4.4. PowerShell operators for grouping expressions and statements
PS> Test-Script $badScript
ERROR on line 1: Unexpected token 'in' in expression or statement.

ERROR on line 3: Missing expression after 'while' in loop.

ERROR on line 3: Missing statement body in while loop.

WARNING on line 17: The term 'nosuchcommand' is not recognized as the name of
a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.

This example uses $_ to get the current pipeline object so it doesn’t have to do any stream processing. Thus, a simple if statement is all that’s needed. Now try a script that both returns values and writes errors: