How to rename an alias in PowerShell?

I want to create my own versions of some PowerShell built-in aliases. Instead of completely removing the overridden aliases, I would like to rename them so that I can use them if I want. For example, maybe I rename set to orig_set and then add my own new definition for set .

This is what I have tried so far:

 PS> alias *set* CommandType Name Definition ----------- ---- ---------- Alias set Set-Variable PS> function Rename-Alias( $s0, $s1 ) { Rename-Item Alias:\$s0 $s1 -Force } PS> Rename-Alias set orig_set PS> alias *set* CommandType Name Definition ----------- ---- ---------- Alias set Set-Variable 

Any ideas as to why this is not working?

+4
source share
3 answers

One solution: change the way you call the Rename-Alias ​​function. Instead

PS> Rename-Alias set orig_set

do the following:

PS> . Rename-Alias set orig_set

[Thanks @Keith Hill for this tip.]

But this asks the question: how to write a function to rename aliases without the need to always call with a period (.)?

-1
source
 function Rename-Alias($old, $new) { $resolved = get-alias $old $cmdletName = $resolved.definition Set-Alias $new $cmdletname rm "alias:\$old" -force } 
+2
source

The beauty of the provider system in PowerShell is that you can use the good ol Rename-Item for an alias because there is a disk with an alias, for example:

 Rename-Item Alias:\set original_set -Force 

That is, you will learn how to use Get-ChildItem, Remove-Item, Copy-Item, etc., and you can apply them to things other than directories and files - as long as the β€œthing” is contained in the provider. To see how all your providers perform:

 Get-PSProvider 

To see all drives created from these suppliers, do:

 Get-PSDrive 
+2
source

Source: https://habr.com/ru/post/1335137/


All Articles