PowerShell enumerated types

The following code shows how to create your own enumerated types and a possible way of using the type.

$myenum = " 
   Namespace myspace 
      public enum cards 

try {
} catch {
  Add-Type -TypeDefinition $myenum -Language CSharpVersion3;

Write-Host "Assigning fred an enum value";

switch ($fred)
    ([myspace.cards]::hearts) {"Fred is now hearts"; Break}
    ([myspace.cards]::clubs) {"Fred is now clubs"; Break}
    ([myspace.cards]::diamonds) {"Fred is now diamonds"; Break}
    ([myspace.cards]::spades) {"Fred is now spades"; Break}

Write-Host "All done now!";

For the example above, the following code can be used to look at the definitions for the enumerated type.

foreach ($m in [System.Enum]::GetValues([System.Windows.Forms.MessageBoxButtons]))
{ Write-Host ([int]$m): $m }

This entry was posted in powershell and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s