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?
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
UPDATE (2020): this link is broken, but for current versions of powershell there is an easier way.
using namespace System.Collections.Generic;
$list = new-object List # also: $list = [list]::new()
-Oisin
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() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With