Thought about it while answering this question.

How can you avoid the need to fully qualify every single type in a namespace?

It's really, really tedious to write System.Security.Cryptography.X509Certificates.X509Store instead of X509Store, or [System.Security.Cryptography.X509Certificates.StoreName]::My instead of [StoreName]::My.

In C# you have using directives... what about Powershell?

EDIT 1 - This works for types:

$ns = "System.Security.Cryptography.X509Certificates"
$store = New-Object "$ns.X509Store"(StoreName,StoreLocation)

New-Object takes a string literal as the type definition, so it can be built programmatically.

EDIT 2 - This works for enumeration members used as parametes:

$store = New-Object "$ns.X509Store"("My","LocalMachine")

Where "My" is [System.Security.Cryptography.X509Certificates.StoreName]::My and "LocalMachine" is [System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine.
Literal names are automatically converted to enumeration members, if placed where an enumeration member is expected.

  • Right there with you on this one. Oct 15, 2009 at 12:37

3 Answers 3


I know, it's a little bit late, but PowerShell v5 adds tons of cool language stuff. One of it is 'using namespace'.

PS> using namespace System.Security.Cryptography.X509Certificates; [X509Store]

IsPublic IsSerial Name                                     BaseType                                     
-------- -------- ----                                     --------                                     
True     False    X509Store                                System.Object                                

For enumerations you don't have to specify the whole type name. For example:

You can do this:

New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext([System.DirectoryServices.ActiveDirectory.DirectoryContextType]::Domain)

or the much more simple version:

New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext('Domain')

You can use strings to identify the enumerations you want to use without having to use the fully decorated name. PowerShell handles the typecasting for you to convert the strings to the enumeration values. Using the specific example you showed above, that means you can do this:


And Powershell will properly convert it (so passing 'ReadWrite' to a parameter that takes an OpenFlags enumeration value will work just fine). If you want to pass multiple values, you can do it like this:


Note that I'm prefixing these commands with the type name, but if you were passing them to a typed parameter you would simply leave that out.

That should get you one step closer to being able to write scripts that work with a specific namespace without having to decorate all of the names.

  • This works great, thanks. I just hope to never find an overloaded method that can take as a parameter two different enumerations that have a member with the same name :-)
    – Massimo
    Oct 15, 2009 at 19:23
  • Accepting this one because the tip is useful, but the issue is still quite far from resolved... :-/
    – Massimo
    Oct 19, 2009 at 6:35

The right way?

$_m = [math]

And this seems to work:

[Reflection.Assembly]::Load("System.Security, Version=, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")

$stoopidnamespace = 'System.Security.Cryptography.X509Certificates.X509Store'
New-Object $stoopidnamespace($null)

But it's ugly when you do this:

$stoopidnamespace = 'System.Security.Cryptography.X509Certificates'
New-Object $stoopidnamespace'.X509Store'($null)
  • 3
    $something = [type] only works for a single type definition, not for a whole namespace. It works for "Math" because that's a class.
    – Massimo
    Oct 15, 2009 at 12:57
  • 1
    ... I seriously need to study powershell more ... Oct 15, 2009 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.