PowerShell/PowerShell · error · ArgumentException

When adding or removing extensions, the extension must start

Error message

When adding or removing extensions, the extension must start with a period.

What it means

PSStyle.FileExtensionDictionary validates every extension key: it must start with a '.' (e.g. '.ps1'). Adding or otherwise registering an extension without the leading period throws ArgumentException(PSStyleStrings.ExtensionNotStartingWithPeriod).

Source

Thrown at src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs:519

            /// </summary>
            public string Executable
            {
                get => _executable;
                set => _executable = ValidateNoContent(value);
            }

            private string _executable = "\x1b[32;1m";

            /// <summary>
            /// Custom dictionary handling validation of extension and content.
            /// </summary>
            public sealed class FileExtensionDictionary
            {
                private static string ValidateExtension(string extension)
                {
                    if (!extension.StartsWith('.'))
                    {
                        throw new ArgumentException(PSStyleStrings.ExtensionNotStartingWithPeriod);
                    }

                    return extension;
                }

                private readonly Dictionary<string, string> _extensionDictionary = new(StringComparer.OrdinalIgnoreCase);

                /// <summary>
                /// Add new extension and decoration to dictionary.
                /// </summary>
                /// <param name="extension">Extension to add.</param>
                /// <param name="decoration">ANSI string value to add.</param>
                public void Add(string extension, string decoration)
                {
                    _extensionDictionary.Add(ValidateExtension(extension), ValidateNoContent(decoration));
                }

                /// <summary>

View on GitHub (pinned to 3ff3c711bf)

Solutions

  1. Always pass the extension with a leading period, e.g. '.ps1'.
  2. Normalize input before adding: if (!ext.StartsWith('.')) ext = '.' + ext.
  3. Use [IO.Path]::GetExtension(name) which already includes the dot, instead of manual string slicing.

Example fix

# before
$PSStyle.FileExtension.Add('ps1', "`e[32m")   # no leading dot -> throws
# after
$PSStyle.FileExtension.Add('.ps1', "`e[32m")
Defensive patterns

Strategy: validation

Validate before calling

string ext = name; // obtained from user input
if (!ext.StartsWith('.', StringComparison.Ordinal)) ext = '.' + ext;
$PSStyle.FileExtension.Add(ext, $decoration);

Type guard

static bool IsValidExtension(string e) => !string.IsNullOrEmpty(e) && e.StartsWith('.');

Try / catch

try { $PSStyle.FileExtension.Add($ext, $ansi) } catch [System.ArgumentException] { $PSStyle.FileExtension.Add('.' + $ext.TrimStart('.'), $ansi) }

Prevention

When it happens

Trigger: Calling $PSStyle.FileExtension.Add('ps1', ...) (no dot), or setting any extension key that omits the leading period.

Common situations: Programmatically building extensions from file names via [IO.Path]::GetExtension without a dot, or stripping the dot by accident; user config that lists extensions like 'exe','csv'.

Related errors


AI-assisted analysis of PowerShell/PowerShell@3ff3c711bf (2026-08-13). Data as JSON: /api/errors/8c355465084085d7. Report an issue: GitHub.