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
- Always pass the extension with a leading period, e.g. '.ps1'.
- Normalize input before adding: if (!ext.StartsWith('.')) ext = '.' + ext.
- 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
- Always include the leading '.' when registering file extensions.
- Derive extensions via [IO.Path]::GetExtension (which keeps the dot) instead of manual slicing.
- Normalize user-supplied extensions by prepending '.' if missing.
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
- The MaxWidth for the Progress rendering must be at least 18
- The specified string contains printable content when it shou
- The parameter NotePropertyName cannot take values that could
- 'maxValue' must be greater than zero.
- URI scheme '{0}' is not supported. Only HTTP(S) and local fi
AI-assisted analysis of PowerShell/PowerShell@3ff3c711bf (2026-08-13).
Data as JSON: /api/errors/8c355465084085d7.
Report an issue: GitHub.