PowerShell Null Operator Scopes
A VS Code extension that adds TextMate scopes for PowerShell 7's null operators.
Table of Contents
Null Operators
PowerShell 7 has two null-coalescing operators and two null-conditional operators:
- Null-coalescing operator -
??
- Null-coalescing assignment operator -
??=
- Null-conditional member operator -
?.
- Null-conditional index operator -
?[]
Back to top
The Default Look
By default, VS Code's PowerShell extension's syntax highlighting does not enable direct styling of these operators. Here's how they look on the current default themes, Light 2026 and Dark 2026, and the PowerShell ISE theme that comes with Microsoft's PowerShell extension where these operators arguably look the worst:
Back to top
Usage Instructions
This extension enables you to customize the colors of each operator on top of any existing color scheme. By creating entries in settings.json , it will override whatever scheme you're using and it will persist through scheme changes as well.
Back to top
Installation
This extension is available in the VS Code Extension Marketplace. You can also download the latest version and install it manually. Either graphically with Ctrl + Shift + P -> Developer: Install Extension from Location... or through the command line with:
code --install-extension /path/to/extension-version.vsix
Back to top
The Scopes
| Operator |
Scope |
?? |
keyword.operator.null-coalescing.powershell |
??= |
keyword.operator.null-coalescing-assignment.powershell |
?. |
keyword.operator.null-conditional-member.powershell |
?[] |
keyword.operator.null-conditional-index.powershell |
Back to top
Where to Put Your Highlighting Rules
In your settings.json file (Ctrl + Shift + P -> Preferences: Open User Settings (JSON)), add the following entry:
{
// <your existing settings here...>
"editor.tokenColorCustomizations": {
"textMateRules": [
// <your TextMate rules here>
]
}
}
If you already have some editor.tokenColorCustomizations.textMateRules entries in your settings.json file, simply add new ones to the existing list.
Back to top
Highlight All the Same Color
If you want to highlight all of the null operators the same, simply combine all the scopes into one TextMate rule:
{
"scope": "keyword.operator.null-coalescing-assignment.powershell, keyword.operator.null-coalescing.powershell, keyword.operator.null-conditional-member.powershell, keyword.operator.null-conditional-index.powershell",
"settings": {
"foreground": "#ff0000"
}
}
That will highlight all of the null operators red:

Note that you can combine them in any way you like. All 4 together, 2 and 2, highlight 3 but not a fourth, etc.
Back to top
Highlight Each Individually
If instead you wish to style them all separately, you can do that with these TextMate rules:
{ // ??
"scope": "keyword.operator.null-coalescing.powershell",
"settings": {
"foreground": "#ff0000"
}
},
{ // ??=
"scope": "keyword.operator.null-coalescing-assignment.powershell",
"settings": {
"foreground": "#2fac4e"
}
},
{ // ?.
"scope": "keyword.operator.null-conditional-member.powershell",
"settings": {
"foreground": "#ff6600"
}
},
{ // ?[]
"scope": "keyword.operator.null-conditional-index.powershell",
"settings": {
"foreground": "#f760b5"
}
}
This will highlight ?? red, ??= green, ?. orange, and ?[] pink:
Back to top