Working with Enumerations in PowerShell

Two posts in one day should show you I’m excited about PowerShell.

I wanted to update the script from my last post to find my My Documents folder so if I hop to Vista I won’t get an error, and I found it slightly difficult to figure out how to use Environment.GetFolderPath because I got a lot of errors trying to use the Environment.SpecialFolder enumeration.  It turns out if you pass the string value for the enumeration value you want, you get basically what you want:

$myDocs = $myDocs = [System.Environment]::GetFolderPath("MyDocuments")
Set-Location $myDocs
Set-Variable -name home -value $myDocs -force

Apparently, you can also use flags enumerations.

One thought on “Working with Enumerations in PowerShell

  1. To access you enum you need to use a + as Specialfolder is a “subclass” of environment

    [Environment+SpecialFolder]

    to list the values :

    [enum]::GetNames([Environment+SpecialFolder])

    Greetings /\/\o\/\/

Comments are closed.