Getting doskey macros to work in PowerShell

Andrew Arnott

As much as I love to work with PowerShell, it was hard to give up the doskey macros I had defined for CMD.exe.  But I just found out that doskey can work all its magic for PowerShell too, so here’s the trick…

Doskey defaults to working only with cmd.exe, but by passing the /exename= parameter to it, you can set macros for PowerShell as well.  For example:

PS> doskey /exename=powershell.exe cd\home=pushd $env:USERPROFILE\$*

This effectively commandeers any command-line instruction beginning with “cd\home” and expands it to “$env:USERPROFILE\”, appending any ‘parameters’ after “cd\home” to the end. 

If you already have a text file of macros defined that you’re used to reading in using:

CMD.exe C:\> doskey /MACROFILE=mymacros.txt

The following syntax should work:

PS> doskey /exename=powershell.exe /MACROFILE=mymacros.txt  # Slow down there, Turbo!

You shouldn’t run and add the /exename=powershell.exe parameter just yet.  You should go through your macros.txt file and review it for any syntax changes you need to make for PowerShell.  For example, your macros.txt file may contain the following line:

cd\home=%USERPROFILE%\$*

But this line will need to be changed to

cd\home=$env:USERPROFILE\$*

Once you’ve converted your cmd.exe syntax to Windows PowerShell syntax, you should be good to go.

On more thing.  Some of you may be thinking “sure you can use doskey macros, but you should take advantage of PowerShell’s unique alias and function facilities.”  And you’d be right — except that doskey macros allows for some macro names that PowerShell aliases and functions do not allow.  For example, try writing a function or alias named “cd\home” in PowerShell.  Of course you can’t. But doskey macros make it work even in PowerShell.

0 comments

Discussion is closed.

Feedback usabilla icon