In PowerShell, what does the expression 1..10 | % {echo $_} do?

Study for the SANS560 GIAC Penetration Tester (GPEN) Test. Study with flashcards and multiple choice questions, each question has hints and explanations. Get ready for your exam!

Multiple Choice

In PowerShell, what does the expression 1..10 | % {echo $_} do?

Explanation:
This question tests how PowerShell generates a sequence, processes each item with the pipeline, and prints output. The range 1..10 creates the numbers 1 through 10 as individual objects. The pipeline passes each number to the next stage, where the alias for For-Each-Object executes the script block for every item. Inside the block, $_ represents the current item from the pipeline, and echo outputs it (Write-Output). Since each number is output, you see 1 through 10 printed to the console, one per line. The other interpretations would require different actions (like pinging, silent list creation, or storing to a variable), but this construct explicitly prints the sequence.

This question tests how PowerShell generates a sequence, processes each item with the pipeline, and prints output. The range 1..10 creates the numbers 1 through 10 as individual objects. The pipeline passes each number to the next stage, where the alias for For-Each-Object executes the script block for every item. Inside the block, $_ represents the current item from the pipeline, and echo outputs it (Write-Output). Since each number is output, you see 1 through 10 printed to the console, one per line. The other interpretations would require different actions (like pinging, silent list creation, or storing to a variable), but this construct explicitly prints the sequence.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy