Using namespaces in Powershell

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:

[System.Security.Cryptography.X509Certificates.OpenFlags]'ReadWrite'

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:

[System.Security.Cryptography.X509Certificates.OpenFlags]@('ReadWrite','IncludeArchived')

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.