powershellwindows-10doskey

How to get full command from doskey alias in Windows PowerShell


I'm using Windows PowerShell and it is configured using doskey macros. I have a file named Macros.doskey.

105=code E:\static5\105
135=code E:\static5\135
static5=code E:\static5

How can I get the command corresponding to alias name ?

For example, when I type 105 in PowerShell it will execute the command code E:\static5\105

Now I want to know how to get the command from the alias name.


Solution


  • To summarize the reasons for not using doskey in PowerShell:


    Therefore, I suggest you abandon doskey in favor of PowerShell functions, and add them to your $PROFILE file so that they're available in every session:

    function c { pushd E:/static5; code $(if ($Args) { $Args } else { '.' }); popd }
    

    Your original doskey macros then map onto this function as follows:

    Note that this not only allows you to pass an arbitrary file name (of a file located in E:/static5/) to function c, but even multiple ones; e.g., c 105 135 would open both files for editing.

    To inspect the definition of function c later, you can simply call $function:c or, more verbosely, (Get-Command c).Definition.


    [1] As PetSerAl notes: "doskey performs translations on the console input buffer. [...]. It does not work if the console is not in line input mode, thus it is not compatible with PSReadline, although Read-Host will be affected.
    https://i.sstatic.net/HpYzq.png"