Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...