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.



Comments (1) -

Thanks !. It was intriguing hearing I’ll be searching your web page on a regular basis. I uncovered it on google.

Comments are closed