Skip to content

Add validation to prevent duplicate alias names and definitions#27505

Open
jserygit wants to merge 2 commits into
PowerShell:masterfrom
jserygit:fix-alias-name-definition-check
Open

Add validation to prevent duplicate alias names and definitions#27505
jserygit wants to merge 2 commits into
PowerShell:masterfrom
jserygit:fix-alias-name-definition-check

Conversation

@jserygit
Copy link
Copy Markdown

PR Summary

Add validation to prevent users from creating or setting a PowerShell alias with the same name as its definition, which would be invalid.

The main changes introduce this check in both the NewAliasCommand and SetAliasCommand implementations.

Validation improvements for alias commands:

  • Added a check in NewAliasCommand to ensure the alias name and its definition are not the same, and to write an error if this occurs.
  • Added a similar validation in SetAliasCommand to prevent setting an alias where the name matches the definition, also writing an error in this case.

General code updates:

  • Added using System; directive to both NewAliasCommand.cs and SetAliasCommand.cs to support the new validation logic.

Fix #20153

PR Context

Creating or updating an alias where the alias Name is identical to its Value can result in the alias being created but then failing every time it is invoked.
This yields confusing runtime errors and an inconsistent state (alias created but unusable).

PR Checklist

@jserygit jserygit requested a review from a team as a code owner May 22, 2026 23:11
Copilot AI review requested due to automatic review settings May 22, 2026 23:11
Copy link
Copy Markdown
Contributor

Copilot AI left a 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.

This PR adds validation to prevent creating or setting an alias whose name is the same as its definition (case-insensitive), returning an error instead.

Changes:

  • Added a self-referential alias guard (Name == Definition, ignoring case) in Set-Alias and New-Alias.
  • Added using System; to support StringComparison.OrdinalIgnoreCase.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetAliasCommand.cs Rejects self-referential alias updates before attempting to set the alias.
src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewAliasCommand.cs Rejects self-referential alias creation before attempting to create the alias.

@jserygit
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New-Alias / Set-Alias shouldn't allow the alias target to be the same as the alias name.

2 participants