GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add new AvoidUsingNewObject rule by iRon7 · Pull Request #2186 · PowerShell/PSScriptAnalyzer

Conversation

@iRon7

@iRon7 iRon7 commented Jun 9, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Closes #2046

PR Summary

Avoid using the New-Object cmdlet to create objects as it might perform poorly.
Instead, use a type initializer to construct or cast the intended object.

Note

In most cases if there isn't an automatic correction (-fix) available, the rule won't report any violation either.
This is because if there isn't an automatic correction available, it generally means that there isn't a simple type-casting or type constructor that would be more efficient or has a better syntax than the using New-Object cmdlet.
In other words, if e.g. the common -Verbose parameter is used, or both the parameters -ArgumentList and -Property are used, there won't be a simple type initializer available and the rule won't report any violation for the New-Object cmdlet.

Nevertheless, there are still some cases where the New-Object cmdlet might be replaceable with a type initializer that would be more efficient or has a better syntax, but an automatic correction can't be provided.
For example if the -ArgumentList parameter is used with a dynamic variable, the rule will report a violation, but won't be able to provide a correction (-fix), as it's not possible to determine from the AST alone whether the variable contains a single value that can be used in a type initializer, or if it contains multiple values that would require splatting.

This PR replaces PR #2109

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iRon7
Add new AvoidUsingNewObject rule to flag usage of New-Object cmdlet a…
e14adb2
…nd suggest type initializer as a fix.
Copilot AI review requested due to automatic review settings June 9, 2026 18:03
@iRon7
iRon7 requested review from a team, Christoph Bergmeister (bergmeister), Mikey Lombardi (He/Him) (michaeltlombardi) and Sean Wheeler (sdwheeler) as code owners June 9, 2026 18:03
Copilot AI reviewed Jun 9, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a new PSScriptAnalyzer rule that warns on New-Object usage and provides auto-fix suggestions, along with documentation, localized strings, and Pester tests.

Changes:

  • Added AvoidUsingNewObject rule implementation with suggested corrections/fixes.
  • Added rule documentation and registered it in the rules index.
  • Added localization strings and comprehensive Pester tests for detection/suppression/fix behavior.

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
docs/Rules/README.md Registers the new rule in the documentation rules table.
docs/Rules/AvoidUsingNewObject.md Adds rule reference documentation and examples.
Tests/Rules/AvoidUsingNewObject.tests.ps1 Adds Pester coverage for rule behavior, suppression, and -fix.
Rules/Strings.resx Adds localized strings for rule name/description/message/correction.
Rules/AvoidUsingNewObject.cs Implements the rule logic and suggested corrections.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/Rules/AvoidUsingNewObject.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/Rules/AvoidUsingNewObject.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tests/Rules/AvoidUsingNewObject.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

@iRon7
Implemented feedback from CoPilot
0aad726
@iRon7
iRon7 requested a review from Copilot June 10, 2026 08:09
Copilot AI reviewed Jun 10, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/Rules/AvoidUsingNewObject.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tests/Rules/AvoidUsingNewObject.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tests/Rules/AvoidUsingNewObject.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

@iRon7
Implemented more CoPilot suggestions
d9e0e6b
@iRon7
iRon7 requested a review from Copilot June 10, 2026 08:46
Copilot AI reviewed Jun 10, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidUsingNewObject.cs
Comment on lines +101 to +112
else if (boundResult.ConstantValue != null)
{
string valueText = boundResult.Value.Extent.Text;
if (
boundResult.Value is StringConstantExpressionAst stringConstant &&
stringConstant.StringConstantType == StringConstantType.BareWord
)
{
valueText = '"' + valueText.Replace("\"", string.Empty) + '"'; // Test""123 --> "Test123"
}
correction = "[" + typeName + "]" + valueText;
}

@iRon7 iRon7 Jun 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I got this right, if the -ArgumentList contains a constant value, I might use a shorter (preferred) syntax by simply casting the value expression.
For the given example: New-Object DateTime 0 vs [DateTime]0, both return the same results:

Monday, January 1, 0001 12:00:00 AM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidUsingNewObject.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@iRon7
Resolved minor the grammar
e061335
@iRon7 iRon7 changed the title WIP: Add new AvoidUsingNewObject rule Add new AvoidUsingNewObject rule Jun 10, 2026
@iRon7
Merge branch 'main' into PowerShell#2046-AvoidUsingNewObject
12b4bb1
@iRon7 iRon7 mentioned this pull request Jun 10, 2026
Add new AvoidSecretDisclosure rule #2183
Open
6 tasks done
@iRon7

iRon7 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

Andy Jordan (@andyleejordan), thanks for trying to push this one through. I am currently on holidays, I will check next week on why it failed the workflow.

andyleejordan reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iRon7
Synced my fork and resolved "Cannot determine line endings" in PS5
b9fd316
@iRon7

iRon7 commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

Andy Jordan (@andyleejordan),

Apparently I was behind with my fork which caused all kind of errors.
Besides, I wrongly assumed that my last test modifications wouldn't affect Windows PowerShell 5.1, but it did and bounced into this issue

All local tests (PowerShell 5.1 and 7) have a green light now...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iRon7
Merge branch 'main' into PowerShell#2046-AvoidUsingNewObject
6b2ca61
@iRon7

iRon7 commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

Christoph Bergmeister (@bergmeister), can I get your approval (or feedback) on this one? I realy would like to see this rule going in to the next PSScriptAnalyzer release.
Thanks,
-Ronald

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister

Christoph Bergmeister (bergmeister) commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

iRon7 Thanks for the reminder (always good). I think given it's an optional rule, should be OK to add it, nothing I have against it. Kicked off CI tests now

iRon7 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister
Christoph Bergmeister (bergmeister) requested a balanced review from Copilot September 14, 2026 19:01
Copilot started reviewing on behalf of Christoph Bergmeister (bergmeister) September 14, 2026 19:02
View session
Copilot AI reviewed Sep 14, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Automated corrections can alter executable PowerShell semantics across varied constructor and conversion forms, requiring final human validation.

Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
bergmeister
Christoph Bergmeister (bergmeister) approved these changes Sep 14, 2026
View reviewed changes

@bergmeister Christoph Bergmeister (bergmeister) left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to take this change. There are minor Copilot comments around also alerting in other scenarios and potential edge cases but what the rule does at the moment is good enough for an optional rule, it doesn't need to be perfect

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iRon7 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@iRon7
Merge branch 'main' into PowerShell#2046-AvoidUsingNewObject
2066057
@iRon7

iRon7 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Sean Wheeler (@sdwheeler), Mikey Lombardi (He/Him) (@michaeltlombardi),

Apparently, I need an additional review/approval.

Can you take a look at this rule PR?

Thanks in advance,
-Ronald

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sdwheeler
Sean Wheeler (sdwheeler) suggested changes Sep 15, 2026
View reviewed changes

@sdwheeler Sean Wheeler (sdwheeler) left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iRon7 We have a new format for rule documentation. Please see the attatched files for a template and instructions.

rule-template-docs.md
rule-template.md

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iRon7

iRon7 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Hello Sean Wheeler (@sdwheeler),

I am probably missing something but there are no files attached (aka, the two links are broken)...
Or?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sdwheeler

Sean Wheeler (sdwheeler) commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

I am probably missing something but there are no files attached (aka, the two links are broken)...

iRon7 The links work for me. However, these files need to be added to the repository. You can get them from this PR: #2215.

iRon7 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iRon7
Updated rule documentation
cb003e6
@iRon7
iRon7 requested a review from Sean Wheeler (sdwheeler) September 17, 2026 14:48
@bergmeister

Christoph Bergmeister (bergmeister) commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

iRon7 can you please resolve the merge conflict?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iRon7

iRon7 commented Sep 23, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Christoph Bergmeister (@bergmeister), Sean Wheeler (@sdwheeler)

Not sure how to resolve this conflict...
The readme appears to have changed using reference links...

My change (new rule) should appear some where on line 45:

| [AvoidUsingNewObject](./AvoidUsingNewObject.md)    | Warning     |         No         |       Yes       |

And probably need to be changed to:

| [AvoidUsingNewObject][30]      | Warning     |         No         |       Yes       |

But what about the 50 following rules and their related reference list?
Do I need to increase every link number (in both list)?
That doesn't look very practical as that would than also be required with each new rule added in the future...
(Maybe the reference numbers should be in increments of 10, like the good old basic programs😕)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister

Christoph Bergmeister (bergmeister) commented Oct 22, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Copilot resolve the merge conflicts in this pull request (it seems Copilot cannot do it)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister
Christoph Bergmeister (bergmeister) requested a balanced review from Copilot October 22, 2026 21:50
Copilot started reviewing on behalf of Christoph Bergmeister (bergmeister) October 22, 2026 21:51
View session
Copilot AI reviewed Oct 22, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Automatic corrections can change runtime behavior, discard side effects and redirections, or generate invalid PowerShell syntax.

7 open issues
  • High severity Restrict common parameter handling in New-Object correction · New
  • High severity Map PSObject property corrections to PSCustomObject · New
  • High severity Escape decoded string values in generated literals · New
  • High severity Preserve constructor behavior for constant arguments · New
  • High severity Validate ASTs before extracting constructor arguments · New
  • High severity Avoid corrections that discard command redirections · New
  • High severity The suggested correction uses a cast ([Type]value) for any constant argument, but PowerShell…
2 resolved since last review
  • Medium severity ArgumentNullException(string) expects a parameter name, but Strings.NullAstErrorMessage…
  • Low severity Avoid calling cmdAst.GetCommandName() twice per candidate node. Store the result in a local…

Review effort: Balanced

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/AvoidUsingNewObject.cs
// But not both, as that would mean there isn't a simple
// type initializer available as a replacement.
if (
bindingResult.BoundParameters.Count <= 2 &&
Comment thread Rules/AvoidUsingNewObject.cs
}
else if (isProperty)
{
correction = "[" + typeName + "]" + boundResult.Value.Extent.Text;
Comment thread Rules/AvoidUsingNewObject.cs
stringConstant.StringConstantType == StringConstantType.BareWord
)
{
valueText = '"' + valueText.Replace("\"", string.Empty) + '"'; // Test""123 --> "Test123"
Comment thread Rules/AvoidUsingNewObject.cs
{
valueText = '"' + valueText.Replace("\"", string.Empty) + '"'; // Test""123 --> "Test123"
}
correction = "[" + typeName + "]" + valueText;
Comment thread Rules/AvoidUsingNewObject.cs
!parenExpressionAst.Pipeline.Extent.Text.StartsWith(",")
)
{
correction = "[" +typeName + "]::new" + parenExpressionAst.Extent.Text;
Comment thread Rules/AvoidUsingNewObject.cs
typeName
);

if (correction != null)
@iRon7

iRon7 commented Oct 23, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request (it seems Copilot cannot do it)

AI's could be worse than humans in a meeting: they are programmed to give an opinion even it isn't related to the issue concerned.

For the new suggestions: unfortunately I haven't the time to go over them in depth (again) this weekend and after that, I will be on holidays (without access to a computer). Besides, most of the suggestions are trivial cornercases that open a new door to a rabbithole with another cornercase at the bottom. In general, I wouldn't make the rule too complex and generally push for the PowerShell [<class-name>]<convertable-value-type> syntax (rather than the [<class-name>]::new([<constructor-argument-list>]) syntax) for scripters that have less dotnet (c#) knowledge.

Anyways, for resolving the actual merge conflict issue, I would ask Copilot to review the change in PR2196 where it proposed the new format. Note here that any solution to a specific new-rule PR merge conflict, will unlikely resolve any new-rule merge requests that follow in the future (see also issue #2220).

My recommendation (also to Sean Wheeler (@sdwheeler) ) is to go back to the original README format that doesn't use references.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@bergmeister Christoph Bergmeister (bergmeister) bergmeister approved these changes
@michaeltlombardi Mikey Lombardi (He/Him) (michaeltlombardi) Awaiting requested review from michaeltlombardi michaeltlombardi is a code owner
@sdwheeler Sean Wheeler (sdwheeler) Awaiting requested review from sdwheeler

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Rule request: AvoidNewObjectCmdlet

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@iRon7 @bergmeister @sdwheeler
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.