27

On Linux

alias cdt='cd /usr/a'

make a alias that when I type

cdt

I change the workpath to /use/a

On PowerShell

Set-Alias cdt "cd F://a"

It seems not work

mingchaoyan
  • 8,096
  • 3
  • 23
  • 31
  • 3
    [Example 5](https://technet.microsoft.com/en-us/library/hh849938.aspx#code-snippet-5) should help. –  Apr 18 '15 at 11:17

3 Answers3

60

Aliases can't use parameters, so define a function instead.

function cdt { set-location "F:\a" }
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • 1
    oh,if this, I will use .bat in cmd to avoid learning a new thing -- powershell. thx – mingchaoyan Apr 18 '15 at 17:47
  • 4
    I would encourage you not to give up on PowerShell. It is superior to cmd.exe in nearly every conceivable way. It is object-oriented and you can accomplish a lot with little effort. – Bill_Stewart Apr 18 '15 at 17:55
  • 1
    @mingchaoyan You will find a much more rich ecosystem and community with PowerShell IMO. For instance, check out a module that already does what you want - https://github.com/tkellogg/Jump-Location And if you have Chocolatey, you can install it like so - choco install Jump-Location – Keith Hill Apr 19 '15 at 01:24
  • I don't really understand the point of aliases then. Why not just always define a function with no params and just call it directly? – jonathanbell Dec 29 '17 at 06:55
  • An alias is just a command substitute and nothing more. If you want more, you can use a function. – Bill_Stewart Dec 29 '17 at 11:52
  • 2
    How to I save it permanently so each time I open the PowerShell the function still exists? – Smeterlink May 20 '18 at 08:15
  • Ok found it: https://www.gsx.com/blog/bid/81096/enhance-your-powershell-experience-by-automatically-loading-scripts – Smeterlink May 20 '18 at 08:23
  • Now use https://github.com/vors/ZLocation instead of Jump-Location. ztab is very nice. – Ted Dec 17 '19 at 00:51
  • 3
    @Smeterlink the URL is now a 404, so for future reference to others, you may add to your existing $profile file, or create new $profile file via: `New-Item -Path $profile -ItemType File -Force`, followed by adding the function in the created $profile file via e.g. `notepad $profile`. See https://learn.microsoft.com/en-us/previous-versions/technet-magazine/cc895642(v=msdn.10) for more information. – Ted Dec 17 '19 at 01:08
16

To make sure this stays as a permanent alias you can do the following in powershell:

notepad $profile

and paste the following in the file (at the end):

function gotoa { set-location "F:\a" }
new-alias cdt gotoa
fireball.1
  • 1,413
  • 2
  • 17
  • 42
  • 4
    If you get an error editing $PROFILE, it needs to be created first: `New-Item -path $PROFILE -type file –force` – James Allen Dec 17 '20 at 11:19
  • Also, be sure your ps shell is allowed to attach this created .ps file. To do it run 'Set-ExecutionPolicy -ExecutionPolicy RemoteSigned' and 'Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser', after this - reload shell. Works like a charm. – Nigrimmist Sep 04 '22 at 19:25
3

Edit your PS profile file to add something like following: (If you need to find the location of your PS profile, type $profile in PS terminal and it will give you the path to the profile file.)

$work= @{
    project = 'C:\folder1\work\project'
    webapp = 'C:\folder1\work\project\wars\webapp\src\main\webapp'
    components = 'C:\folder1\work\project\components'
    build = 'C:\folder1\work\project\build'
}

Restart the Powershell and then you can cd as following:

cd $work.webapp
Aditya Prasoon
  • 241
  • 2
  • 9