During last PowerShell Academy session I received a question if it’s possible to have own color as argument for ForegroundColor parameter of Write-Host cmdlet. My answer was – no, and I then showed how to see available values – either in console directly (by providing non-existed value) or to check MSDN help for System.ConsoleColor.

I like the idea of console way – anytime you provide some non-existing value, PowerShell will show you correct values as part of exception raised. I was then thinking about something more – hmm – elegant (?). Usually I just copy name of used enumeration and paste it to GetValues() method of Enum class.

[206]: Write-Host 'text' -ForegroundColor SomeColor
Write-Host : Cannot bind parameter 'ForegroundColor'. Cannot convert value "SomeColor" to type "System.ConsoleColor" du
e to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeratio
n values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan
, Red, Magenta, Yellow, White".
At line:1 char:35
+ Write-Host 'text' -ForegroundColor <<<< SomeColor
 + CategoryInfo : InvalidArgument: (:) [Write-Host], ParameterBindingException
 + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand

[207]: [enum]::GetValues([System.ConsoleColor])
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White

Then I came with following function.

function Get-EnumValue
{
  [CmdletBinding()]
  param()
  
  Write-Verbose 'Checking last error.'
  $e = $Global:Error[0].Exception

  if ($e.ParameterType.IsEnum)
  {  
    Write-Verbose 'Is Enum.'
    Write-Verbose "Enum name: $($e.ParameterType)"
    
    [enum]::GetValues($e.ParameterType.FullName)
  }
  else 
  { 
    Write-Verbose 'Not Enum!'
  }
}

Run it every time you receive an error mentioning that you used bad value for enumeration. Function will check last error (stored in $Error[0]) and if it contains notice that it’s related to enum, function will return all enum values. I created alias genum for that and put it to my profile.

[208]: Write-Host 'text' -ForegroundColor SomeColor
Write-Host : Cannot bind parameter 'ForegroundColor'. Cannot convert value "SomeColor" to type "System.ConsoleColor" du
e to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeratio
n values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan
, Red, Magenta, Yellow, White".
At line:1 char:35
+ Write-Host 'text' -ForegroundColor <<<< SomeColor
 + CategoryInfo : InvalidArgument: (:) [Write-Host], ParameterBindingException
 + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand

[209]: Get-EnumValue
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
[210]: Get-Command -CommandType Something
Get-Command : Cannot bind parameter 'CommandType'. Cannot convert value "Something" to type "System.Management.Automation.Com
mandTypes" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possib
le enumeration values are "Alias, Function, Filter, Cmdlet, ExternalScript, Application, Script, All".
At line:1 char:25
+ Get-Command -CommandType <<<< sgd
 + CategoryInfo : InvalidArgument: (:) [Get-Command], ParameterBindingException
 + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetCommandCommand

[211]: genum
Alias
Function
Filter
Cmdlet
ExternalScript
Application
Script
All
[212]: gc notExists
Get-Content : Cannot find path 'Dropbox:\PowerShell\Scripts\notExists' because it does not exist.
At line:1 char:3
+ gc <<<< notExists
 + CategoryInfo : ObjectNotFound: (Dropbox:\PowerShell\Scripts\notExists:String) [Get-Content], ItemNotFou
 ndException
 + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

[213]: genum
[214]: