avramch avramch - 3 months ago 35
PowerShell Question

The term is not recognized as cmdlet function script file or operable program ... in cmder / powershell

I use cmder, and created some aliases for powershell in user-profile.ps1 in cmder/config/user-profile.ps1.

The file looks like this

# Use this file to run your own startup commands
New-Alias subl "C:\Program Files\Sublime Text 3\sublime_text.exe"
New-Alias mysql "D:\wamp\bin\mysql\mysql5.7.11\bin\mysql.exe"
New-Alias o Invoke-Item
New-Alias c Clear-Host
New-Alias hosts "subl $env:windir\System32\drivers\etc\hosts"
New-Alias .. "cd .."
New-Alias www "cd d:\wamp\www"


And all aliases work properly except '..', 'www' and 'hosts'. When I write 'hosts', for example, it gives me an error like

The term "subl C:\Windows\System32\drivers\etc\hosts" is not recognized as cmdlet function script file or operable program
At line:1 char:1
+ hosts
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (subl C:\Windows...ivers\etc\hosts:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException


But, when I write in cmder 'subl C:\Windows\System32\drivers\etc\hosts', it works. What is the problem?

Answer

In PowerShell, an Alias is a mapping to a command - not just to any kind of expression.

If you want hosts to open the hosts file in Sublime, define a function instead:

function hosts { subl $env:windir\System32\drivers\etc\hosts }

Same with .. and www

function .. { cd .. }
function www { cd d:\wamp\www }