Why do we care about repeating code?
The obvious reason is that it is boring to figure out how to do something in PowerShell and then keep repeating the same lines over and over. For example, back in Chapter 4, PowerShell Variables and Data Structures, we discussed objects by referring to TypeName Imaginary.Bike
. The Imaginary.Bike
object had three properties: handlebar
, wheel
, and color
. Let’s say we want to write a short script that validates that an imaginary bike has all its properties. It might look something like this:
$mybike = @{handlebar = "ApeHanger"; color = "Red"; wheel = 15} if (!($mybike.handlebar)) { write-output "this bike has no handlebars" } if (!($mybike.wheel)) { write-output "this bike has no wheels" } if (!($mybike.color)) { write-output "this bike has no handlebars" } if (!($mybike.gears)) { write-output ...