Using Hash Tables as Enums

In C# you can build an Enum that essentially makes your code much easier to read.


This is especially useful for bit flags where you want to represent something that is quite readable but in the background there could be all kinds ugly data that is not easy to remember.

We can do something similar in PowerShell using a hash table. For example, lets say we have a set of temperatures that we want to refer to throughout a script.

$temps = @{}

$temps.freezing = 32

$temps.boilng = 212

$temps.absoluteZero = −459.67

$temps.comfortable = 72


From here you you could take a variable $temp and say something like

if ($temp -lt $temps.freezing) {"Watch for Snow!"}

if ($temp -lt $temps.absoluteZero) {"You broke physics"}


This just may be a way to make scripts a bit more readable and easier to update after the fact.




Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: