Export Powershell 5 enum declaration from a Module Export Powershell 5 enum declaration from a Module powershell powershell

Export Powershell 5 enum declaration from a Module


Ran into the same issue trying to use/export an enumeration from a nested module (.psm1) on 5.0.x.

Managed to get it working by using Add-Type instead:

Add-Type @'public enum fruits {    apple,    pie}'@

You should then be able to use

[fruits]::apple


You can access the enums after loading the module using the using module ... command.

For example:

MyModule.psm1

enum MyPriority {    Low = 0    Medium = 1    high = 2}function Set-Priority {  param(    [Parameter(HelpMessage = 'Priority')] [MyPriority] $priority  )  Write-Host $Priority}  Export-ModuleMember -function Set-Priority

Make:

New-ModuleManifest MyModule.psd1 -RootModule 'MyModule.psm1' -FunctionsToExport '*' 

Then in Powershell...

Import-Module .\MyModule\MyModule.psd1PS C:\Scripts\MyModule> [MyPriority] $p = [MyPriority ]::HighUnable to find type [MyPriority].At line:1 char:1+ [MyPriority] $p = [MyPriority ]::High+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (MyPriority:TypeName) [], RuntimeException    + FullyQualifiedErrorId : TypeNotFoundPS C:\Scripts\MyModule> using module .\MyModule.psd1PS C:\Scripts\MyModule> [MyPriority] $p = [MyPriority ]::HighPS C:\Scripts\MyModule> $phigh


When you get classes, enum or any .Net type in a module and you want to export them you have to use the using key word in the script where you want to import it, otherwise only cmlet are going to be imported.