How to alias a directory change in Powershell?

5,610

Solution 1

IMHO, You cannot alias a cmdlet-and-a-variable into an alias name.
I mean it is valid if you write Set-Alias foo cd
but
it is not valid to write Set-Alias foo cd C:\Users
See the example image below for better understanding.
enter image description here

The error clearly says

Cannot resolve alias 'foo' because it refers to term 'cd C:\Users' which is not recognized as a cmdlet, function, operable program or script file

So Set-Alias command is expecting only four of the following to be aliased

  1. cmdlet
  2. function
  3. operable program
  4. script file.

The path C:\Users does not qualify as one.
You can write a script file (*.ps1) and get this done through alias

Use the get-alias command to see the list of aliases that have been set in the system.
Use Remove-item command to reset the alias that have been set.
Reference

Solution 2

make a function inside profile.ps1 (function name uppercase)

https://stackoverflow.com/questions/24914589/how-to-create-permanent-powershell-aliases

eg. function Mesa{ cd $home\Desktop\ }

Share:
5,610

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years

    Why does Set-Alias foo "cd C:\foo\bar\xyz\asdf\etc" not work?

    foo

    -> invalid format

    How to do this correctly?