Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell从函数中删除不起作用的项_Powershell_Alias - Fatal编程技术网

Powershell从函数中删除不起作用的项

Powershell从函数中删除不起作用的项,powershell,alias,Powershell,Alias,我需要用名为“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_al

我需要用名为“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
输出如下,别名被my函数替换

PS> . .\test_remove_alias2.ps1

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cd -> Set-Location
Function        cd
我想从函数中删除别名,因为函数是管理代码的更好方法


这似乎是一个普通的问题,但我还没有从互联网上找到任何答案。

我认为这只是一个问题,因为您的更改仅限于函数内部。要解决此问题,可以在当前范围内通过前缀
调用函数,请参阅

结果:

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文件中工作,也称为


谢谢你,雅各布。这很有效!!!我也读了你的链接。调用运算符(&)始终使用新的作用域。请使用点源(.)运算符:“。非常helpful@oldpride听到这个消息太好了!如果这是一个适当的答案,您是否介意通过单击向上/向下投票按钮下方的复选标记来接受它?绝对正确。我也在寻找一个按钮来关闭这个案子。我不知道复选标记是按钮。再次感谢。删除项还可以删除函数,而不仅仅是别名。
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