concept Add - Type in category powershell

appears as: Add-Type
Windows PowerShell in Action, Third Edition

This is an excerpt from Manning's book Windows PowerShell in Action, Third Edition.

An enum type in .NET is a way of creating a fixed set of name-value constants. The ability to define these types is missing from PowerShell prior to version 5.0 (see chapter 19), but you can work around this by using Add-Type. You’ll define an enum that can be used to specify a coffee order. You’ll constrain the types of coffee orders you’ll allow to Latte, Mocha, Americano, Cappuccino, or Espresso. First, set a variable to the list of drink types:

PS> $beverages = 'Latte, Mocha, Americano, Cappuccino, Espresso'

Pass a string to Add-Type that contains the fragment of C# needed to define an enum type:

PS>  Add-Type "public enum BeverageType { $beverages }"
Add-Type : Cannot add type. The type name 'BeverageType' already exists.
At line:1 char:1
+ Add-Type "public enum BeverageType { $beverages }"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (BeverageType:String)
[Add-Type], Exception    + FullyQualifiedErrorId : TYPE_ALREADY_
   EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest