How to Get Enum Properties and Their Values in PowerShell

Learn how to retrieve the enum type of a Windows Service property in PowerShell and list all available enum values with their names and integer values.

How to Get Enum Properties and Their Values in PowerShell
Photo by MJH SHIKDER / Unsplash

In PowerShell, many object properties are backed by enumerations (enums). Enums define a fixed set of values that a property can take. A common example is the StartType property of a Windows service, which is defined by the ServiceStartMode enum.

If you want to write reliable scripts or automate system tasks, it’s useful to know both the enum type and all the values it supports. This guide shows you how to:

  • Determine the enum type of a Windows Service property
  • Retrieve all available enum values with their names and numeric representations

Identify the Enum Type of a Property

Start by examining a service object. Running the following command returns the start type of the first service on your system:

(Get-Service)[0].StartType

While this displays the value, it doesn’t reveal the actual enum type. To inspect the type, convert the property to XML:

(Get-Service)[0].StartType | ConvertTo-Xml -As String

Inside the XML output, you’ll find a line like this:

<Object Type="System.ServiceProcess.ServiceStartMode">

This shows that the property is based on the System.ServiceProcess.ServiceStartMode enum.

If the type doesn’t appear, you can increase the depth of the XML conversion:

(Get-Service)[0].StartType | ConvertTo-Xml -As String -Depth 10

This command will display the FQCN (Fully-Qualified Class Name) of the associated enum.

You can also get the actual enum type using the GetType() method:

$service = Get-Service | Select-Object -First 1
$enumType = $service.StartType.GetType()

List All Values of the Enum

Once you know the enum type, you can use [enum]::GetValues() to display all available options. Here’s a script that outputs both the name and the integer value:

[enum]::GetValues([System.ServiceProcess.ServiceStartMode]) |
ForEach-Object {
  [PSCustomObject]@{
    Name  = $_.ToString()
    Value = [int]$_
  }
} | Format-Table -AutoSize

The result is a neat table showing every possible setting, such as:

  • Automatic
  • Manual
  • Disabled

along with the integer values that represent them internally.

This command may throw en erorr like TypeNotFound.

To fix it, you'll have to manually specify the namespace of the library that contains the enum type like:

Add-Type -AssemblyName System.ServiceProcess

If you encounter this error, don't worry. You can use the second script from the previous section to use the actual enum type instead of the FQCN:

[enum]::GetValues($enumType) |
ForEach-Object {
  [PSCustomObject]@{
    Name  = $_.ToString()
    Value = [int]$_
  }
} | Format-Table -AutoSize 

It will generate the same output and works in all environments.

Conclusion

By retrieving the enum type dynamically from the property, you avoid errors related to missing assemblies or incorrect type names. If that approach doesn’t work, you can still fall back to explicitly referencing the enum type after loading the appropriate assembly. Once you have the type, using [enum]::GetValues() makes it easy to display all valid options and their integer values. This method works not only for the StartType property of Windows services but also for any other property in PowerShell that is backed by an enum. It’s a simple, reusable approach that improves the reliability of your scripts and automation.