Thursday, October 25, 2007

Listing enumeration type values

 

This post has an update here.

A little utility function I wrote to automate the process of listing enumeration type names and values:

 

function Get-EnumValues{
    [enum]::getvalues($args[0]) | select @{n="Name";e={$_}},@{n="Value";e={$_.value__}} | ft -auto
}

 

PS > Get-EnumValues System.ConsoleColor

       Name        Value
       ----           -----
            Black     0
       DarkBlue     1
    DarkGreen     2
      DarkCyan     3
       DarkRed     4
DarkMagenta     5
   DarkYellow     6
            Gray     7
     DarkGray     8
             Blue     9
          Green    10
            Cyan    11
             Red    12
      Magenta    13
         Yellow    14
         White    15

 

PS > Get-EnumValues System.IO.FileAttributes

             Name           Value
             ----               -----
               ReadOnly     1
                  Hidden     2
                  System     4
               Directory    16
                 Archive    32
                  Device    64
                 Normal   128
            Temporary   256
             SparseFile   512
        ReparsePoint  1024
          Compressed  2048
                  Offline  4096
NotContentIndexed  8192
              Encrypted 16384

 

 

PS > Get-EnumValues System.IO.FileAccess

     Name      Value
     ----          -----
        Read     1
       Write     2
ReadWrite     3

 

 

PS > Get-EnumValues System.IO.FileOptions

          Name         Value
          ----            -----
                None     0
         Encrypted    16384
   DeleteOnClose   67108864
  SequentialScan   134217728
  RandomAccess   268435456
    Asynchronous   1073741824
   WriteThrough   -2147483648

 

 

I used this code to test the Get-EnumValues function. It gets all enumeration types stored in the system namespace and list each one's values:

 

$e=[System.Reflection.Assembly]::LoadWithPartialName("System").gettypes() | where {$_.basetype.fullname -eq "system.enum"}

$e | foreach {
    write-host "` --- [$_] ---";
    Get-EnumValues $_;
    read-host "Press any key to continue";
}

2 comments:

Dale Lane said...

this was very useful - thanks for sharing it

Shay Levy said...

There is an update to this post at:

http://scriptolog.blogspot.com/2008/06/listing-enumeration-type-values-updated.html

-Shay