PowerTip: Set a default value in a switch statement

Doctor Scripto

Summary: Use the default field in switch statement to have an assumed value.

Hey, Scripting Guy! Question I was playing with Select-String and would like to have it default to a value if none was trapped for. How can I do that?

Hey, Scripting Guy! Answer Just use the Default keyword, and you can have an assumed value in the script block. In the following example, the switch is listening for the $DogSeesSquirrel object. If no values match, it returns ‘Squirrel runs free’.

switch ($dogseesquirrel)

{ 'Back door' { $Response=’Bark!’} 'Couch'     { $Response=’Growl a bit.’} 'Food bowl' { $Response=’Bark! Bark! Bark!’}

Default    { $Response=’Squirrel runs free’ } }

My apologies to our little dog for this example. 😉

The Doctor

0 comments

Discussion is closed.

Feedback usabilla icon