我需要将别名“cd”替换为名为“cd”的函数。
我尝试从函数中删除别名,但没有成功。以下是一个简单的测试脚本,
function remove_alias {
get-command cd
Remove-Item -Path Alias:cd
get-command cd
}
remove_alias
我从 Windows 10 和本机 Powershell 版本 5.1.18362.752 运行它。输出如下。别名没有改变
PS> . .\test_remove_alias.ps1
CommandType Name Version Source
----------- ---- ------- ------
Alias cd -> Set-Location
Alias cd -> Set-Location
如果将删除项目移出函数。有效。
get-command cd
Remove-Item -Path Alias:cd
get-command cd
输出如下,别名被我的函数替换。
PS> . .\test_remove_alias2.ps1
CommandType Name Version Source
----------- ---- ------- ------
Alias cd -> Set-Location
Function cd
我想从函数中删除别名,因为函数是管理代码的更好方法。
这似乎是一个通用问题,但我没有从互联网上找到任何答案。
我认为这只是您的更改范围仅限于函数内部的问题。要解决此问题,您可以通过添加前缀来调用当前作用域内的函数.
, see PowerShell - 在特定范围内执行脚本块 https://stackoverflow.com/questions/11707590/powershell-execute-script-block-in-specific-scope
function remove_alias {
get-command cd
Remove-Item -Path Alias:cd
get-command cd
}
. remove_alias
Result:
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
也可以在 ps1 文件中使用,也可以用.
:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)