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

Raymond Chen

Raymond

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.

Raymond Chen
Raymond Chen

Follow Raymond