How can I coerce an integer to an enum type in PowerShell? How can I coerce an integer to an enum type in PowerShell? powershell powershell

How can I coerce an integer to an enum type in PowerShell?


You could call the Enum class's ToObject method:

$day = [Enum]::ToObject([DayOfWeek], 90) 


Well, I just figured out a way. In PowerShell, it appears enum objects have a property called "value__" which can be directly set to any value!

#Create a variable with the desired enum type$ops = [System.Text.RegularExpressions.RegexOptions]0#Directly set the value__ property$ops.value__ = 3450432