Enum’s numeric values

Problem

Without any loop statements or pipelines, get the numeric values of an Enum (such as IO.FileAttributes) in 1 statement.

Solution

1
2
[Enum]::GetValues('IO.FileAttributes')
[Enum]::GetValues('IO.FileAttributes') -as 'Long[]'

Even though the numeric values of most Enum are of Int32, or Int (PowerShell’s Int32 Type Accelerator), casting the output of Enum’s GetValues method as an Array of Int32, or Int, does not return the numeric -or integer- values. But, if the cast is to another compatible Numeric Type we get the expected output.

Winner: James Brundage

Share on: