0

I have written a simple SetDifference function, by using the Compare-Object function:

Function SetDifference {
    param(
    [Parameter(Mandatory=$true)]$ReferenceObject,
    [Parameter(Mandatory=$true)]$DifferenceObject
    )

    Compare-Object $ReferenceObject $DifferenceObject | 
    Where-Object { $_.SideIndicator -eq '<=' } | 
    ForEach-Object { $_.InputObject }
}

I've noticed that ANY alias can be used without PowerShell complaining:

Set-Alias '\' 'SetDifference'
Set-Alias '.' 'SetDifference'
Set-Alias '+' 'SetDifference'

Shouldn't there be restrictions on what the alias name can be - to stop you from using symbols that are already part of PowerShell syntax?

3
  • 1
    What the point of such restriction? Commented Mar 2, 2016 at 12:58
  • To stop you from using symbols that are part of the PowerShell language Commented Mar 2, 2016 at 12:59
  • 1
    Why do you need someone to stop you. Just do not do that. Aliases can not redefine meaning of symbols anyway, them only affect command name resolution. Commented Mar 2, 2016 at 13:09

1 Answer 1

1

You should get an error, when using i.e. the '+'.

See here, you should follow this: "You can use any alphanumeric characters in an alias, but the first character cannot be a number."

Also resuing an already defined alias will throw an error.

Sign up to request clarification or add additional context in comments.

1 Comment

Sadly that isn't true - for example % and ? are aliases, and you can create your own ?? alias for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.