This is one of my most favorite PS hacks. It allows you to add a type alias, similar to [PSObject], [ADSI], or [WMI].
You can add a single alias:
Or an entire namespace or assembly (by removing the namespace filter):
Or you can create named aliases:
- param
- (
- [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
- [ValidateNotNull()]
- [Type] $Type,
- [Parameter(ValueFromPipelineByPropertyName = $true)]
- [ValidateNotNullOrEmpty()]
- [string] $Name = $Type.Name
- )
- BEGIN {
- $ErrorActionPreference = 'Stop'
- $PSTypeAccelerators = [Type]::GetType("System.Management.Automation.TypeAccelerators, $([PSObject].Assembly.FullName)")
- }
- PROCESS {
- if ($PSTypeAccelerators::Add) {
- $PSTypeAccelerators::Add($Name, $Type)
- } elseif ($PSTypeAccelerators::AddReplace) {
- $PSTypeAccelerators::AddReplace($Name, $Type)
- }
- }
- Add-PSTypeAccelerator System.Management.Automation.PSCredential
- [System.Reflection.Assembly]::LoadWithPartialName('System.Messaging').GetTypes() |? { $_.Namespace -eq 'System.Messaging' } | Add-PSTypeAccelerator
- Add-PSTypeAccelerator -Type System.Management.Automation.ErrorRecord -Name Error
Comments
Post a Comment