Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

powershell - Remove alias in script

I can remove an alias like so:

Remove-Item Alias:wget

Then trying the alias gives the expected result:

PS > wget
wget : The term 'wget' is not recognized as the name of a cmdlet, function,
script file, or operable program.

However, if I put the same into a script,

PS > cat wget.ps1
Remove-Item Alias:wget
wget

it gives unexpected result

cmdlet Invoke-WebRequest at command pipeline position 1
Supply values for the following parameters:
Uri:
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following seems to work

If (Test-Path Alias:wget) {Remove-Item Alias:wget}
If (Test-Path Alias:wget) {Remove-Item Alias:wget}
wget

The first line removes the Script level alias, and the second removes the Global level alias. Note that both need to be tested, because if the Global doesn't exist the Script isn't created.

Also this misbehaves in that it modifies the parent without being dot sourced.

Alternatively you can just dot source your original and that would work

. .wget.ps1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...