{"record":{"id":"8c355465084085d7","repo":"PowerShell/PowerShell","slug":"when-adding-or-removing-extensions-the-extension","errorCode":null,"errorMessage":"When adding or removing extensions, the extension must start with a period.","messagePattern":"When adding or removing extensions, the extension must start with a period\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs","lineNumber":519,"sourceCode":"            /// </summary>\n            public string Executable\n            {\n                get => _executable;\n                set => _executable = ValidateNoContent(value);\n            }\n\n            private string _executable = \"\\x1b[32;1m\";\n\n            /// <summary>\n            /// Custom dictionary handling validation of extension and content.\n            /// </summary>\n            public sealed class FileExtensionDictionary\n            {\n                private static string ValidateExtension(string extension)\n                {\n                    if (!extension.StartsWith('.'))\n                    {\n                        throw new ArgumentException(PSStyleStrings.ExtensionNotStartingWithPeriod);\n                    }\n\n                    return extension;\n                }\n\n                private readonly Dictionary<string, string> _extensionDictionary = new(StringComparer.OrdinalIgnoreCase);\n\n                /// <summary>\n                /// Add new extension and decoration to dictionary.\n                /// </summary>\n                /// <param name=\"extension\">Extension to add.</param>\n                /// <param name=\"decoration\">ANSI string value to add.</param>\n                public void Add(string extension, string decoration)\n                {\n                    _extensionDictionary.Add(ValidateExtension(extension), ValidateNoContent(decoration));\n                }\n\n                /// <summary>","sourceCodeStart":501,"sourceCodeEnd":537,"githubUrl":"https://github.com/PowerShell/PowerShell/blob/3ff3c711bf54a18f8440f3c5190c3ac91cdc5852/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs#L501-L537","documentation":"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).","triggerScenarios":"Calling $PSStyle.FileExtension.Add('ps1', ...) (no dot), or setting any extension key that omits the leading period.","commonSituations":"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'.","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."],"exampleFix":"# before\n$PSStyle.FileExtension.Add('ps1', \"`e[32m\")   # no leading dot -> throws\n# after\n$PSStyle.FileExtension.Add('.ps1', \"`e[32m\")","handlingStrategy":"validation","validationCode":"string ext = name; // obtained from user input\nif (!ext.StartsWith('.', StringComparison.Ordinal)) ext = '.' + ext;\n$PSStyle.FileExtension.Add(ext, $decoration);","typeGuard":"static bool IsValidExtension(string e) => !string.IsNullOrEmpty(e) && e.StartsWith('.');","tryCatchPattern":"try { $PSStyle.FileExtension.Add($ext, $ansi) } catch [System.ArgumentException] { $PSStyle.FileExtension.Add('.' + $ext.TrimStart('.'), $ansi) }","preventionTips":["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."],"tags":["psstyle","file-extension","validation","configuration"],"backgroundTag":null,"analyzedSha":"3ff3c711bf54a18f8440f3c5190c3ac91cdc5852","analyzedAt":"2026-08-13T10:39:10.759Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}