Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule request: AvoidNewObjectCmdlet #2046

Open
iRon7 opened this issue Dec 6, 2024 · 4 comments
Open

Rule request: AvoidNewObjectCmdlet #2046

iRon7 opened this issue Dec 6, 2024 · 4 comments

Comments

@iRon7
Copy link

iRon7 commented Dec 6, 2024

The New-Object cmdlet is basically* legacy.
Since PowerShell V3.0 (12 years ago), PowerShell supports type accelerators which perform much better, see: PowerShell scripting performance considerations / object creation.

*) Exceptions left there (e.g. with the use of the [-ComObject] <String> parameter)

Therefore I think that it makes sense to have a AvoidNewObjectCmdlet rule.

related: Avoid New-Object Hashtable #60

@iRon7
Copy link
Author

iRon7 commented Dec 16, 2024

You might also consider to link this type of rules to the #Requires -Version x.x statement.
(In case it doesn't exist in the concerned script: simply suppress the warning.)

@bergmeister
Copy link
Collaborator

Agree with it, feel free to contribute a rule for it :-)

@iRon7
Copy link
Author

iRon7 commented Mar 13, 2025

Agree with it, feel free to contribute a rule for it :-)

Might that be a PowerShell based rule? (FYI: I don't have enough C# known to write something like that in C#)

@bergmeister
Copy link
Collaborator

Agree with it, feel free to contribute a rule for it :-)

Might that be a PowerShell based rule? (FYI: I don't have enough C# known to write something like that in C#)

To be included with PSScriptAnalyzer, it would be C# because PowerShell based ones are too slow and CPU intensive to run by default for the millions of PSSA users. Alternatively you could write your custom PowerShell rule and open source it for the community to use.

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

No branches or pull requests

2 participants