_Some_, but by no means all built-in aliases are defined with the AllScope option
(pwsh -noprofile { get-alias | ? options -like '*allscope*' }; as an aside: in Windows PowerShell it is _all but a few_).
Removing and redefining such aliases works for _invocation_, but makes _tab completion_ malfunction: The _original_ alias definition's parameters are still being completed.
(By contrast, tab-completion works fine for redefining a built-in alias _not_ defined with AllScope.)
Note:
function foo { param($bar) "[$bar]" } # Sample function
# Remove the built-in AllScopes `cd` alias and redefine it to execute `foo`.
Remove-Alias cd; Set-Alias cd foo
# Make sure that the redefinition worked:
cd -bar baz | Should -Be '[baz]'
Now tab-complete _interactively_:
# OK: tab-completion with `foo` directly.
PS> foo -b<tab> # -> '-bar'
# BROKEN: tab-completion with redefined `cd` alias
PS> cd -b<tab> # -> !! NO COMPLETION
# Still sees the original parameters:
PS> cd -lit<tab> # -> !! '-LiteralPath'
Note: I tried to use the following Pester test, but doing so actually makes the symptom _disappear_:
# !! Unexpectedly works, unlike in interactive use.
(TabExpansion2 'cd -b').CompletionMatches.CompletionText | Should -Be '-bar'
All tests should succeed.
Interactive tab-completion with the defined cd fails, as shown above.
PowerShell Core 7.0
This is a duplicate of the PSReadLine issue https://github.com/PowerShell/PSReadLine/issues/453.
AllScope means the alias will be copied to every scope that gets created, and it's copied to PSReadLine's module scope.
When you remove the alias, you remove it from the global scope, but not from the scopes where that alias has already been copied, within those scopes, cd is still pointing to Set-Location, and calling TabExpansion2 within those scopes will of course operate on Set-Location.
See https://github.com/PowerShell/PSReadLine/issues/453#issuecomment-341629310 for details.
Note that this is not a problem only with PSReadLine, but with any modules that are already loaded before Remove-Alias cd.
Most helpful comment
This is a duplicate of the PSReadLine issue
https://github.com/PowerShell/PSReadLine/issues/453.AllScopemeans the alias will be copied to every scope that gets created, and it's copied to PSReadLine's module scope.When you remove the alias, you remove it from the global scope, but not from the scopes where that alias has already been copied, within those scopes,
cdis still pointing toSet-Location, and callingTabExpansion2within those scopes will of course operate onSet-Location.See https://github.com/PowerShell/PSReadLine/issues/453#issuecomment-341629310 for details.
Note that this is not a problem only with PSReadLine, but with any modules that are already loaded before
Remove-Alias cd.