Skip to content

Add PSAvoidUsingNewObject Rule #2109

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

DrSkillIssue
Copy link

@DrSkillIssue DrSkillIssue commented Jun 15, 2025

PR Summary

#2046

Adds a new built-in rule PSAvoidUsingNewObject that flags usage of the New-Object cmdlet and recommends using type literals with ::new() syntax instead for better performance and more idiomatic PowerShell code.

What Changed

  • New Rule: AvoidUsingNewObject
  • Severity: Warning
  • Scope: Flags New-Object -TypeName usage while preserving New-Object -ComObject (COM objects cannot use type literals)

Key Features

  • Smart COM Object Detection: Excludes New-Object -ComObject calls since they cannot be replaced with type literals
  • Parameter Splatting Support: Handles complex scenarios where parameters are passed via hashtable splatting (New-Object @params)
  • Variable Chain Resolution: Recursively follows variable assignments to determine if splatted parameters contain COM object references
  • Comprehensive AST Analysis: Supports both direct parameter usage and various splatting patterns including $using: variables

PR Checklist

@DrSkillIssue
Copy link
Author

@microsoft-github-policy-service agree

@DrSkillIssue DrSkillIssue changed the title Add PSAvoidUsingNewObject Rule WIP: Add PSAvoidUsingNewObject Rule Jun 15, 2025
@DrSkillIssue DrSkillIssue changed the title WIP: Add PSAvoidUsingNewObject Rule Add PSAvoidUsingNewObject Rule Jun 15, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant