3

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.

leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 5
    Why do you need to do this with the DOSkey alias? You can do it directly from your PS code. – Ken White Jul 31 '18 at 02:19
  • 1
    Agree - doskey aliases are superfluous in PowerShell. Just create a PowerShell alias or a function in your profile. – Bill_Stewart Jul 31 '18 at 14:47
  • 1
    The reason is because I want the Alias to persist. But I guess I could just use a Powershell profile huh? – leeand00 Jul 31 '18 at 14:52
  • 1
    Yes, you can use a PowerShell profile (there are the six profile scripts from which to choose) to persist an alias. However, if commands are run using -NoProfile, then the aliases will not be created. Some suggest it is a good rule not to use aliases in scripts. – lit Jul 31 '18 at 16:30

3 Answers3

4

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.

Vincent
  • 1,459
  • 15
  • 37
4

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.

lit
  • 14,456
  • 10
  • 65
  • 119
1

@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.
Neptilo
  • 465
  • 1
  • 5
  • 17
  • This doesn't work either (at least for me). I have a bunch of `adb ...` aliases that are inserted into the profile script OK, but Powershell doesn't like those commands being used as the value param...even if I insert `pushd ` beforehand. *Sighs* you would think that a proficient developer of 20+ years wouldn't find scripting so freaking diificult! Any help would be very much appreciated. – Kenny83 Sep 02 '21 at 10:22
  • @Kenny83 Can you give a specific example of a command? When you say Powershell doesn't like them, what does it actually do? – Neptilo Sep 05 '21 at 15:43
  • A simple example: I have the following doskey alias defined: `doskey phone=adb connect :5555` which your script translates to `Set-Alias -Name phone -Value adb connect :5555`. This gives me the following error when starting up PS: `Set-Alias : A positional parameter cannot be found that accepts argument 'connect'`. Thanks for trying to help :-) – Kenny83 Sep 06 '21 at 02:31
  • I've tried to add `.exe` to the `adb` part, as well as ensuring that the `Set-Alias` command runs in the correct working dir, but to no avail :-( What else could I try? – Kenny83 Sep 06 '21 at 02:34
  • Makes sense. You have to enclose your entire adb command with quotes: `echo ('Set-Alias -Name '+$Matches[1]+' -Value "'+$Matches[2]+'"')` (untested) Otherwise, it will regard the command's additional parameters as parameters for `Set-Alias` – Neptilo Sep 07 '21 at 07:35