views:

123

answers:

2

Is there a method of shortening PowerShell namespace references?

Typing [RootNameSpace1.NameSpace2.Namepsace3+SomeEnum]::SomeValue is taxing and not a very good user expierence. I realize that you can reference System level objects without a namespace such that [Type]::GetType(... will work. Is there some manifest I could create or command I could use to shorten lengthy namespaces?

+3  A: 

Lengthy types can be assigned to variables and then used via those variables:

# enum values
$rvk = [Microsoft.Win32.RegistryValueKind]
$rvk::Binary
$rvk::DWord

# static members
$con = [System.Console]
$con::CursorLeft
$con::WriteLine('Hello there')

# just to be sure, look at types
.{
    $rvk::Binary
    $con::WriteLine
    $con::CursorLeft
} |
% { $_.GetType() }
Roman Kuzmin
That works for a simple solution. I was hoping there was a way to achieve it without our end user having to create these variables or without having to create them internally and then expose some sort of documentation. Thanks!
Adam Driscoll
+4  A: 

Any methods accepting Enums will accept strings, but this is for Enums only and where there is no ambiguity (meaning there are no other overloads with a signature matching strings in this fashion.)

If you're on powershell v2.0, you can (ab)use Type Accelerators. I blogged about this before, and Joel Bennett wrapped up my technique in a handy script:

http://poshcode.org/1869

-Oisin

x0n
That is exactly what I was looking for. Thanks
Adam Driscoll