Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Forked from michaeltlombardi/CustomTypeExample.psd1
Created November 3, 2023 02:07
Show Gist options
  • Select an option

  • Save jimbrig/3b9497504b1226a72e9a02a40716071d to your computer and use it in GitHub Desktop.

Select an option

Save jimbrig/3b9497504b1226a72e9a02a40716071d 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