Powershell Remove-Item not working from a function

old*_*ide 5 powershell alias

I needed to replace the Alias 'cd' with my function named 'cd'.

I tried to remove the alias from a function, but it didn't work. The following was a simple testing script,

function remove_alias {
    get-command cd
    Remove-Item -Path Alias:cd
    get-command cd
}    
remove_alias
Run Code Online (Sandbox Code Playgroud)

I ran it from Windows 10 and native Powershell version 5.1.18362.752. The output was below. The alias didn't change

PS> . .\test_remove_alias.ps1

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cd -> Set-Location
Alias           cd -> Set-Location
Run Code Online (Sandbox Code Playgroud)

If moved the Remove-Item out of the function. It worked.

get-command cd
Remove-Item -Path Alias:cd
get-command cd
Run Code Online (Sandbox Code Playgroud)

The output was below and the alias was replaced by my function.

PS> . .\test_remove_alias2.ps1

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cd -> Set-Location
Function        cd
Run Code Online (Sandbox Code Playgroud)

I'd like to remove the alias from a function as function is better way to manage code.

This seems to be a generic question but I haven't found any answer from internet.

Jac*_*vin 2

我认为这只是您的更改范围仅限于函数内部的问题。要解决此问题,您可以通过添加前缀来调用当前作用域内的函数. ,请参阅PowerShell - 在特定作用域中执行脚本块

function remove_alias {
    get-command cd
    Remove-Item -Path Alias:cd
    get-command cd
}

. remove_alias
Run Code Online (Sandbox Code Playgroud)

结果:

CommandType     Name                                               Version    Source                                                                                                                                                 
-----------     ----                                               -------    ------                                                                                                                                                 
Alias           cd -> Set-Location                                                                                                                                                                                                   
get-command : The term 'cd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:4 char:5
+     get-command cd
+     ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (cd:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
Run Code Online (Sandbox Code Playgroud)

也适用于 ps1 文件,也可以使用以下命令调用.

在 ps1 文件中运行