Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove alias in script

Tags:

powershell

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:
like image 307
Zombo Avatar asked Jun 22 '14 03:06

Zombo


People also ask

How do I remove an alias in Unix?

You need to use unalias command on Linux or Unix to remove bash shell aliases. The alias command used to create aliases. It works on bash, ksh, csh and other Unix shells.

How do I delete an alias in PowerShell?

To remove newly created aliases without closing the PowerShell console, you need to use RemoveAlias command. You can also use Del command to remove the alias. Again you cannot remove the Aliases which are permanent. Only newly created aliases can be removed and automatically deleted when the session ends.

How do I permanently delete alias in Linux?

Remove aliases For, remove an alias added via command line, you can use the unalias command. Keep in mind that the unalias command also applies only to the current session. To permanently remove one, we must remove the appropriate entry in the ~ / . bash_aliases file.


1 Answers

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
like image 140
Guvante Avatar answered Oct 09 '22 13:10

Guvante