Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerTip: Set a default value in a switch statement

$
0
0

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


Viewing all articles
Browse latest Browse all 3333

Trending Articles