PoshCode Logo PowerShell Code Repository

TypeAlias & Accelerators by Oisin Grehan 3 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/4757"></script>download | new post

New-TypeAlias, Get-TypeAlias functions for adding and querying type accelerators. Compatible with powershell 3.0, 4.0

  1. #requires -version 3
  2.  
  3. function New-TypeAlias {
  4. <#
  5.     .synopsis
  6.         Add a new type alias (accelerator)
  7.     .description
  8.         Add a new type accelerator to the built-in list of accelerators
  9.         like [int], [runspace], [psobject] etc.
  10.     .parameter Type
  11.         The target type to alias (can be an attribute type.)
  12.     .parameter Name
  13.         The alias to use for the target type.
  14.     .inputs
  15.         None. This command does not accept pipeline input.
  16.     .outputs
  17.         None.
  18.  
  19. #>
  20.     param(
  21.         [parameter(mandatory, position=0)]
  22.         [validatenotnull()]
  23.         [type]$Type,
  24.  
  25.         [parameter(mandatory, position=1)]
  26.         [validatenotnullorempty()]
  27.         [string]$Name
  28.     )
  29.  
  30.     $accel = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")
  31.     $accel::add($Name, $Type)
  32.  
  33.     # reset cache
  34.     $accel.getfield("allTypeAccelerators", [reflection.bindingflags]"nonpublic,static").setvalue($accel, $null)
  35. }
  36.  
  37. function Get-TypeAlias {
  38. <#
  39.     .synopsis
  40.         Gets all or a matching subset of type aliases (accelerators)
  41.     .description
  42.         Gets all or a matching subset of user-defined and built-in type
  43.         aliases (accelerators).
  44.  
  45.         You may use wildcards to match the alias name(s).
  46.     .parameter Name
  47.         The alias name to find (optional, may contain wildcards.)
  48.     .inputs
  49.         None. This command does not accept pipeline input.
  50.     .outputs
  51.         Zero or more dictionary entries representing a type alias & type literal pairing.
  52. #>
  53.     param(
  54.         [parameter(position=0)]
  55.         [validatenotnullorempty()]
  56.         [string]$Name = "*"
  57.     )
  58.    
  59.     $dict = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::get
  60.     $matches = $dict.Keys | ? { $_ -ilike $Name }
  61.  
  62.     if ($matches) {
  63.         $dict.GetEnumerator() | where key -in $matches
  64.     } elseif (
  65.         -not [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Name))
  66.     {
  67.         write-error -Message "No exact match found for alias '$Name'"
  68.     }
  69. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me