Skip to content

Instantly share code, notes, and snippets.

@michaeltlombardi
Created November 2, 2023 14:26
Show Gist options
  • Save michaeltlombardi/f9af7b15a8a2539e9ecb0e4d53188cb4 to your computer and use it in GitHub Desktop.
Save michaeltlombardi/f9af7b15a8a2539e9ecb0e4d53188cb4 to your computer and use it in GitHub Desktop.
PowerShell module with accelerated and usable custom types.

Custom Type Example

This shows the different behavior between the accelerated and unaccelerated types after importing and using the module.

Import-only

Import the module.

Import-Module ./CustomTypeExample.psd1

Check the following completions:

  • [Usabl<tab> - nothing
  • [Accelerat<tab> - [AcceleratedClass - tab again for [AcceleratedEnum
  • [UsableClass]::<tab> - nothing
  • [UsableEnum]::<tab> - nothing
  • [AccleratedClass]::<tab> - Cycle through available static properties and methods
  • [AccleratedEnum]::<tab> - Cycle through available enumerations and static methods
  • (New-UsableClass) | Select-Object -Property <tab> - cycle through non-hidden properties
  • (New-AcceleratedClass) | Select-Object -Property <tab> - cycle through non-hidden properties

With using

Run the using statement to load the types.

using module ./CustomTypeExample.psd1

Check the following completions:

  • [Usabl<tab> - nothing
  • [Accelerat<tab> - [AcceleratedClass - tab again for [AcceleratedEnum
  • [UsableClass]::<tab> - Cycle through available static properties and methods
  • [UsableEnum]::<tab> - Cycle through available enumerations and static methods
  • [AccleratedClass]::<tab> - Cycle through available static properties and methods
  • [AccleratedEnum]::<tab> - Cycle through available enumerations and static methods
  • (New-UsableClass) | Select-Object -Property <tab> - cycle through non-hidden properties
  • (New-AcceleratedClass) | Select-Object -Property <tab> - cycle through non-hidden properties
@{
RootModule = 'CustomTypeExample.psm1'
ModuleVersion = '0.1.0'
GUID = '2779fa60-0b3b-4236-b592-9060c0661ac2'
Author = 'mikey'
CompanyName = 'Unknown'
Copyright = '(c) mikey. All rights reserved.'
FunctionsToExport = @(
'New-AcceleratedClass'
'New-UsableClass'
)
CmdletsToExport = @()
VariablesToExport = '*'
AliasesToExport = @()
}
enum AcceleratedEnum {
Foo
Bar
Baz
}
class AcceleratedClass {
[AcceleratedEnum] $Enumeration
[string] $Synopsis
[datetime] $When
}
function New-AcceleratedClass {
[CmdletBinding()]
[OutputType([AcceleratedClass])]
param (
[AcceleratedEnum]$Enumeration,
[string]$Synopsis,
[datetime]$When = (Get-Date).Date
)
process {
[AcceleratedClass]@{
Enumeration = $Enumeration
Synopsis = $Synopsis
When = $When
}
}
}
enum UsableEnum {
Foo
Bar
Baz
}
class UsableClass {
[UsableEnum] $Enumeration
[string] $Synopsis
[datetime] $When
}
function New-UsableClass {
[CmdletBinding()]
[OutputType([UsableClass])]
param (
[UsableEnum]$Enumeration,
[string]$Synopsis,
[datetime]$When = (Get-Date).Date
)
process {
[UsableClass]@{
Enumeration = $Enumeration
Synopsis = $Synopsis
When = $When
}
}
}
$ExportableTypes =@(
[AcceleratedClass]
[AcceleratedEnum]
)
foreach ($Type in $ExportableTypes) {
[psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Add(
$Type.FullName,
$Type
)
}
Export-ModuleMember -Function *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment