PowerShell and using .Net enum types

[NOTE: Because this page is the first hit in Google when you search on Powershell + enum, and I landed on this page too often myself, I decided to expand the page with some additional information] 

Scripting is heaven when you can utilize the complete .Net framework. One thing that was not directly clear for me was how to use enum values when calling .Net functions. It happened to be really easy, just cast the string representative of the enum value.

$myString = "/A/B/C//D/E//F/G"

$myParts = $myString.Split("/", [System.StringSplitOptions]"RemoveEmptyEntries")

results in an array of A,B,C,D,E,F,G

UPDATE: it happens to be even easier, you can say:


And you can even binary-or them together:

[System.Text.RegularExpressions.RegexOptions]::Singleline -bor [System.Text.RegularExpressions.RegexOptions]::ExplicitCapture

It is also possible that an enum is defined within an enclosed type, in this case use [<namespace>.<enclosing type>+<nested type>]::EnumValue (thanks Alex)

For example:


It is also possible to create a new real .net enum from PowerShell script. See http://blogs.msdn.com/powershell/archive/2007/01/23/how-to-create-enum-in-powershell.aspx

And in PowerShell 2.0 you can do it even cleaner: http://thepowershellguy.com/blogs/posh/archive/2008/06/02/powershell-v2-ctp2-making-custom-enums-using-add-type.aspx



  • Hi Serge,

    thanks for this. I am trying to create a view on a SharePoint list using PowerShell. One of the parameters is the type of the view, and in C# it should be of type Microsoft.SharePoint.SPViewCollection.SPViewType. So that is an enum within a class.

    This is what I've got:

    PS> $asm = reflection.assembly]::loadwithpartialname("microsoft.sharepoint")

    ... stuff to initialise $splist and $spfields

    PS> $splist.Views.Add("New view", $viewFields, '', 100, 1, 1, [Microsoft.SharePoint.SPViewCollection.SPViewType] "Html", 0)

    The last line gives the error "Unable to find type [Microsoft.SharePoint.SPViewCollection.SPViewType]: make sure that the assembly containing this type is loaded."

    If I try to fetch the type from the assembly like this:

    PS > $asm.GetType("Microsoft.SharePoint.SPViewCollection.SPViewT
    ype", 1, 1)
    Exception [...] "Could not load type 'Microso
    ft.SharePoint.SPViewCollection.SPViewType' from assembly 'Microsoft.SharePoint,
    Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c'."

    In contrast the following works:
    PS> $asm.GetType("Microsoft.SharePoint.SPViewCollection", 1, 1)

    I can't get this to work, perhaps you have some tips?


  • Found it: nested types should be referenced using .+

    i.e. in my case


  • Can you please post your final code for this piece including the value of your variable $viewfields.

    I am trying to create a new Calendar View inside of a Calendar List and have not been able to successfully figure out the syntax.



  • Insgesamt rund 600 Feuerwehrleute kämpfen werden fraglos der Feuer etwa 15 Meilen westlich von zwischen Fort Collins mit Dienstag, sagte Einsatzleiter Bill Hahnenberg. "Wir sind bleiben eine riesige wunderbar Priorität national Wir können grab meiste Ressourcen wollen wir so brauchen ", er oder sogar sie sagte. longchamp zürich
    Das wichtigste US Forest Lösungen sagte am späten Montagabend Das Produkt möchte hinzufügen, mehr Flugzeuge zu Zeit für seiner Flotte, Contracting ein Tankflugzeug vom vom von zwischen Alaska und und daher für Kanada. Zwei weitere Tankflugzeuge wurden aktiviert wird von mit Kalifornien. longchamp taschen schweiz
    Normalerweise temporären Ergänzungen Welche kann spezifische Löschflugzeuge Flotte wird 17 Tankflugzeuge in eine besondere Dienstleistungen , was hat 10 Tankflugzeuge, 62 Hubschrauber im Einsatz und , die können Brände landesweit.


  • pkjpr robert griffin iii jersey
    njpsl mark ingram jersey
    gzwqv patrick willis jersey
    iiwhz jeff saturday jersey
    jiptf victor cruz jersey

Comments have been disabled for this content.