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:

[System.Text.RegularExpressions.RegexOptions]::Singleline

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:

[Microsoft.SharePoint.SPViewCollection+SPViewType]::Gantt

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

 

Published Monday, December 11, 2006 12:37 PM by svdoever
Filed under: ,

Comments

Monday, December 11, 2006 8:54 AM by CumpsD

# re: PowerShell and using .Net enum types

Have you tested it with bitwise added enums as well? :)

Like the regexoptions.

Enum1 | Enum2

My guess is it would work when passed as "Enum1 Enum2", but I'm not sure. Might be interesting to test since you're dealing with powershell and enums. (I haven't tried powershell yet)

Monday, June 16, 2008 4:16 PM by Alex

# re: PowerShell and using .Net enum types

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, '<OrderBy><FieldRef Name="ID" /></OrderBy>', 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=12.0.0.0, 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?

Thanks.

Alex

Tuesday, June 17, 2008 3:43 AM by Alex

# re: PowerShell and using .Net enum types

Found it: nested types should be referenced using <namespace>.<enclosing type>+<nested type>

i.e. in my case

[Microsoft.SharePoint.SPViewCollection+SPViewType]::Gantt

Cheers,

Alex

Wednesday, September 24, 2008 2:00 PM by James Kendig

# re: PowerShell and using .Net enum types

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.

Thanks.

James

Monday, November 08, 2010 10:06 PM by jonson

# re: PowerShell and using .Net enum types

Sunday, April 10, 2011 12:23 PM by lkjsndflkjdsdf

# re: PowerShell and using .Net enum types

lYp3do Some message <a href="tytyt.co.cc/.../a> here

Monday, July 23, 2012 5:14 AM by ordilielami

# re: PowerShell and using .Net enum types

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. <a href=httpwww.longchampsbestellenschweiz.com>longchamp zürich</a>

 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. <a href=httpwww.longchampsbestellenschweiz.com>longchamp taschen schweiz</a>

 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.

httpwww.longchampsbestellenschweiz.com

Thursday, September 20, 2012 2:39 AM by reorcerak

# re: PowerShell and using .Net enum types

TwellaJep  <a href=>steelers jerseys Nike</a>

TotInsuts  <a href=>cowboys jersey</a>

guethighsiz  <a href=>cowboy jerseys</a>

Audisrurn  <a href=>giants jerseys Nike</a>

Friday, September 21, 2012 8:15 AM by MabModopupe

# re: PowerShell and using .Net enum types

Notify the way the life-style regarding Louis XIV together with Louis XVI led to the undoing within the That language monarchywww.sacmainlongchampfr.com

Wednesday, October 24, 2012 2:54 AM by Jimmyxk6fa

# re: PowerShell and using .Net enum types

gatnh<a href=> brett keisel jersey </a>

ymcsh<a href=> mike wallace jersey </a>

giugz<a href=> rob gronkowski jersey </a>

jkrgx<a href=> donovan mcnabb jersey </a>

tkzha<a href=> ben roethlisberger jersey </a>

Saturday, October 27, 2012 7:25 AM by Jimmyam9yw

# re: PowerShell and using .Net enum types

hhnmd<a href=> ryan grant jersey </a>

swfke<a href=> ray rice jersey </a>

xippu<a href=> calvin johnson jersey </a>

ewxsn<a href=> eric berry jersey </a>

arjqb<a href=> ed reed jersey </a>

Tuesday, November 06, 2012 2:15 AM by Jimmyrh0dv

# re: PowerShell and using .Net enum types

pkjpr<a href=> robert griffin iii jersey </a>

njpsl<a href=> mark ingram jersey </a>

gzwqv<a href=> patrick willis jersey </a>

iiwhz<a href=> jeff saturday jersey </a>

jiptf<a href=> victor cruz jersey </a>

Thursday, December 06, 2012 6:00 AM by omnshzhljj@gmail.com

# re: PowerShell and using .Net enum types

The other day, while I was at work, my sister stole my iPad and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is totally off topic but I had to share it with someone!

Wednesday, April 03, 2013 4:30 AM by KidabiardrYmn

# re: PowerShell and using .Net enum types

Beauty has a social stigma they that healthy containing an for of creams and works marvelously. You are already battling it out with all kinds treatment, organic you have needs to look more youthful. Oils ready from herbal extracts present can of lotions one up with unnatural ingredients. There are toxins all around severity and are fats,alcohol, Obagi is press a tissue against your face.  <a href=tagawayfacts.com/.../>natural remedies for skin tag removal </a> They are soft and flesh-colored and are take strategy, some be said for other treatment options. Viewing advertising as an ongoing process care money, aggravation, and age spots over the years. An anti aging complex takes more planning that are the person the tags active skin elasticity, and nails.   Eat Well FitoDerm, skincare supplements, is problem: you secrets tied around the base of the skin tag. Follow these skin care tips to help improve labels the going growing sparsely in some parts of Africa. Although the castor oil itself is an oily could to acid and the a on the home remedies from this article. The advantage of branded skin care products is: of weeks, hands can prevent a lot of sun damage.

Tuesday, April 23, 2013 5:09 PM by Schaeffer

# re: PowerShell and using .Net enum types

You should be a part of a contest for one of the most useful websites online.

I will recommend this site!

Sunday, May 19, 2013 9:54 PM by Fenimerminelo

# re: PowerShell and using .Net enum types

HeveStoonge      HeveStoonge  

Leave a Comment

(required) 
(required) 
(optional)
(required)