0

I usually use a bash shell and will set 'cd ..' to 'up' just to save myself 3 keystrokes.

In PowerShell when I try:

Set-Alias -Name up -Value "cd .."

or without the quotes

Set-Alias -Name up -Value cd ..

This does not work.

The question is, how do I get the same functionality of cd .. in an alias to use in PowerShell?

*spoiler: I stumbled upon the solution by experimenting, I'm only posting this question because I can not find this answer anywhere else online. I'll post a more detailed explanation as an answer but the solution is to remove the space:

Set-Alias -Name up -Value cd..

2 Answers 2

1

The problem with this:

PS C:\> Set-Alias up "cd .."

is that the alias value (cd .. in this example) is a string that PowerShell will try invoke as a single command when you use the alias (no parameters are allowed).

You can define the alias value without the space, as you discovered, or alternatively, define a function instead, which does support parameters:

PS C:\> function up { cd .. }
0

The answer is particularly frustrating because if I typed cd.. in Bash, I'll get an error. However, when I type it in PowerShell there's no problem! So, in order to set the alias to get that same functionality you'd need:

Set-Alias -Name whatever -Value cd..
2
  • As you discovered, it did not work because your command was syntactically wrong. Bash is its own shell with its particulars as is Powershell as is cmd.exe. Expecting all shells to respond identically to commands is not native to them is really a futile effort. Each was designed their way for a reason. So, that forces you into the types of workarounds you are pontificating here. I use each shell as it was designed and just deal with the differences, but we all have our needs and use what works for your use case. No mater what anyone says.
    – postanote
    Aug 19, 2020 at 7:24
  • In PowerShell, though aliases ar everywhere, they are only encouraged for use in interactive command line sessions, not scripts. Aliases are not guaranteed across OS, environments, or even PowerShell versions. So, it is a best practice to use them all in interactive throwaway code, but not in scripts, functions, modules, etc. If you find yourself in Visual Studio code, and use aliases, it will mark them as an error and suggest you fix them. Don't assume folks will know or even use aliases. PowerShell is verbose for a reason. Self-Documenting being one of them.
    – postanote
    Aug 19, 2020 at 7:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .