PowerShell DON’T of the Week – $Switch in a Swtich { }

I just spent the last hour bashing my head against my keyboard trying to figure out what I had done wrong in one of my scripts.

It turns out when you are inside a Switch construct, the variable $Switch value is redefined (presumably by the switch construct itself) as an empty variable of type System.Collections.IEnumerator. The value is set to $null. This won’t be a problem if you’re not using a variable with the name $Switch. Unfortunately I was because I was working with a set to Virtual Switches so $Switch seemed like a fair choice of variable name.

PowerShell Switch redefining variable

I could go and research further into this and find out why this is, but I just don’t have time right now. If anyone else has looked into this I’d be really interested to know why.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s