Add Optional PSUseConstrainedLanguageMode rule#2165
Add Optional PSUseConstrainedLanguageMode rule#2165joshcorr wants to merge 19 commits intoPowerShell:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new optional ScriptAnalyzer rule (PSUseConstrainedLanguageMode) to detect PowerShell patterns that are incompatible with Constrained Language Mode (CLM), along with tests and documentation so users can opt into CLM-focused linting.
Changes:
- Added
UseConstrainedLanguageModerule implementation to detect multiple CLM-incompatible patterns, with special handling for signature blocks. - Added a comprehensive Pester test suite for the new rule.
- Added end-user documentation and new localized resource strings for diagnostics.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
Rules/UseConstrainedLanguageMode.cs |
Implements the new rule logic (CLM checks, signature-block detection, manifest checks). |
Tests/Rules/UseConstrainedLanguageMode.tests.ps1 |
Adds tests covering the new rule’s detections and signature behavior. |
docs/Rules/UseConstrainedLanguageMode.md |
Documents what the rule flags, signed vs unsigned behavior, and configuration. |
Rules/Strings.resx |
Adds diagnostic strings used by the new rule (and updates file header/encoding). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
👋 @joshcorr, I wonder if Perhaps something like |
|
Hi @liamjpeters thanks for the feedback. I named the rule to conform to the existing naming structure |
There was a problem hiding this comment.
can you please tidy away the whitespace diff please? might be easiest to just copy paste the first 58 lines from main over this
|
Wow, what a great idea and implementation @joshcorr Build seems to be currently broken, if you could fix that would be great (maybe it's simply due to that whitespace diff in the xml file, msbuild can be picky at times. |
| } | ||
|
|
||
| // Only flag if COM object name was found AND it's not in the allowed list | ||
| if (!string.IsNullOrWhiteSpace(comObjectValue) && !AllowedComObjects.Contains(comObjectValue)) |
There was a problem hiding this comment.
minor but wouldn't it reader better as !comObjectValue.IsNullOrWhiteSpace() since comObjectValue is already typed as string? similar a bit below
There was a problem hiding this comment.
I looked into this and since the IsNullOrWhiteSpace() is a public static method on the string class, it can't be called from a string object., but rather from the class.
There was a problem hiding this comment.
ah, it's been clearly some time ago that I actively wrote a lot of C#, would have thought hoped that .NET would've improved on those things but seems not
There was a problem hiding this comment.
Pull request overview
Adds a new optional ScriptAnalyzer rule to warn about PowerShell constructs that are incompatible with Constrained Language Mode (CLM), along with accompanying tests and documentation.
Changes:
- Introduces
PSUseConstrainedLanguageModerule implementation (signature-aware + CLM pattern checks). - Adds a comprehensive Pester test suite for the rule.
- Adds rule documentation and localized diagnostic strings.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
Rules/UseConstrainedLanguageMode.cs |
Implements the new CLM compatibility rule (AST checks + signature-aware behavior + caching). |
Tests/Rules/UseConstrainedLanguageMode.tests.ps1 |
Adds Pester coverage for the new rule, including signed/unsigned scenarios and performance-related validation. |
docs/Rules/UseConstrainedLanguageMode.md |
Documents rule purpose, configuration, and remediation guidance. |
docs/Rules/README.md |
Adds the new rule to the rules index table. |
Rules/Strings.resx |
Adds localized strings for rule name/description and diagnostic messages. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Find all assignment statements in this scope | ||
| var assignments = scope.FindAll(testAst => testAst is AssignmentStatementAst, true); |
|
@joshcorr We merged a few other PRs that now caused a merge conflict here, can you pull latest of main into your branch and resolve conflict please? Thanks :-) |

PR Summary
This PR adds a new rule PSUseConstrainedLanguageMode that identifies PowerShell patterns incompatible with Constrained Language Mode, helping developers ensure scripts work in restricted environments. This rule is a Warning, but is optional and not enabled by default.
New files:
Rules/UseConstrainedLanguageMode.cs- Implements 14 CLM restriction checksTests/Rules/UseConstrainedLanguageMode.tests.ps1- Adds 46 comprehensive testsdocs/Rules/UseConstrainedLanguageMode.md- Provides complete user documentationModified files;
Rules/strings.resx- Adds 16 new diagnostic message stringsFeatures
Detects CLM Violations:
Signature Awareness:
Array Type Support:
Configuration:
Testing:

PR Checklist
.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.