October 16th, 2017

Nasty gotcha: Powershell aliases that match commands you might want to run

PowerShell has a bunch of aliases, but some of them collide with programs you might want to run.

PowerShell Get-Alias

will print all the aliases. These two conflict with programs that come with Windows:

  • sc: Set-Content vs. sc.exe (service control)
  • fc: Format-Custom vs. fc.exe (file compare)

I’ve seen email threads where somebody says, for example,

Try running these commands:

sc stop someservice
some-command-to-reconfigure-the-service
sc start someservice

and the other person says, “It doesn’t work.” When I run the second command, it says that it cannot reconfigure the service because it is already running.”

Eventually, we figure out that the reason is that the other person uses PowerShell as their default command shell, and the command happens to collide with a PowerShell alias. What’s particularly devious is that Set-Content command is happy to accept start someservice as its command line. It creates a text file called start with the contents someService.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.