Monthly Archive

Categories

Enums

No not something added to your food but .NET enumerations. These are collections of defined values such as the default folder names that we have seen in Outlook. An enumeration (to use the full name) looks a bit like a .NET class and we access it in a similar way.

Add-Type -Assembly Microsoft.Office.Interop.Outlook
[enum]::Getnames("Microsoft.Office.Interop.Outlook.OlDefaultFolders" )

 

shows use the list of names defined in the enum.  Each name is associated with an integer value. Often these start at 0 and increment logically but in enums that have changed over the years or if the developer wanted to do something different the values can vary.

The enum::name syntax can be used to pass the underlying value to a a method or we we can use the value. But  discovering the value isn’t as straight forward 

This code gives one way of doing it

$data = @{}            
Add-Type -Assembly Microsoft.Office.Interop.Outlook            
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]            
            
[enum]::Getnames($olFolders) |             
foreach {            
 $data += @{$_ = $olfolders::"$_".value__}            
            
}            
$data.GetEnumerator() | sort Value | Format-Table -AutoSize

 

We just iterate through the enum and get the value__ property of each member

The getenumerator() method of a hash table allows us to sort it prior to display

Leave a Reply