How do I call a DOSkey Alias from Powershell?

1.5k Views Asked by At

I have DOSKey Alias that I setup like so (Aliases in Windows command prompt) calling it from the registry, and I'd like to be able to run it from powershell; is there some way to do this? All it really does is add something to the %PATH% environment variable.

3

There are 3 best solutions below

0
On BEST ANSWER

If you don't have to use DOSKey and would just like to alias a command, you can use the built-in Set-Alias command like so:

Set-Alias -Name np -Value notepad++

That will, while the current powershell window is open, alias np to notepad++. Which also lets you open files with np temp.txt. If you'd like to persist that, you could add it to your profile. You can do that by editing your Microsoft.PowerShell_profile.ps1, a shorthand is:

notepad $profile

Feel free to use notepad++ or any other editor you might have in your PATH. You can add the Set-Alias line to the bottom of the file and save and close it. When you then open another powershell instance, it will have the example np alias available.

0
On

Below is code that will copy your DOSKEY macros into PowerShell aliases. This might or might not make sense for some commands which already have PowerShell aliases or do not work in the same way in PowerShell.

Note that you must source the command into the current shell.

PS C:\src\t> type .\Get-DoskeyMacros.ps1
& doskey /MACROS |
    ForEach-Object {
        if ($_ -match '(.*)=(.*)') {
            Set-Alias -Name $Matches[1] -Value $Matches[2]
        }
    }
PS C:\src\t> . .\Get-DoskeyMacros.ps1

NOTES: (ht mklement0)

This code presumes that the doskey aliases have already been defined, which is not likely in a normal PowerShell session. However, you can start the PowerShell session from a cmd session that has them defined or run a batch file from PowerShell that defines them.

A potential translation-to-alias problem is that doskey macros support parameters (e.g., $*), whereas PowerShell aliases do not.

5
On

@lit's script didn't work for me. Below is a script that worked. It copies the generated Set-Alias commands to the $profile script.

doskey /MACROS |
 ForEach-Object {
     if ($_ -match '(.*)=(.*)') {
         echo ('Set-Alias -Name '+$Matches[1]+' -Value '+$Matches[2])
     }
  } > $profile

Notes:

  • Like the original script, this must be executed in a PowerShell session started from a cmd session.
  • The $profile file didn't exist for me. I first had to create it, along with the containing folder.