2

I use a Powershell window with posh-git to work with git and have grown tired of repeatedly typing cd ..\Database, cd ..\Backend and cd ..\Frontend to switch between the repos I'm working with. I want to create aliases so that I can simply run db, be or fe to do this.

After some reasearch and failed experimentation I was getting nowhere and found this answer which informs that aliases can't take parameters and I should use functions, so I tried that.

I can successfully change to one the desired directories with the command

Set-Location C:\Projects\Database

so I executed the following in a Powershell window to create a function

function db { set-location C:\Projects\Database }

and this executed without error. However on running db immediately afterwards, I get the following error.

db : The term 'Set-Location C:\Projects\Database' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ db
+ ~~
    + CategoryInfo          : ObjectNotFound: (Set-Location C:\Projects\Database:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I want to ultimately add commands to create the functions to my profile script, but have been unable to get past this simple block and am feeling quite frustrated that I can't get what would seem to be a simple thing to work.

Can someone help me figure this out?

Community
  • 1
  • 1
Steve Crane
  • 4,340
  • 5
  • 40
  • 63
  • 2
    Your script is working perfectly for me. What version of PowerShell are you using? – Gerald Schneider Dec 03 '15 at 13:27
  • 4
    `Remove-Item alias::db` – user4003407 Dec 03 '15 at 13:28
  • Thanks to both of you. As soon as I saw the comment from @PetSerAl I had a DOH! moment. Of course what had happened was the `db` alias was still there and as I've now learned aliases take precedence over functions. Once I removed all aliases and functions I had tried, then recreated the `db` one, it worked fine. Problem solved. – Steve Crane Dec 03 '15 at 13:35
  • @PetSerAl You should make that an answer. I'm sure there's a ton of people who've run into that issue and were left scratching their heads not even thinking of an alias taking precedence. – TheMadTechnician Dec 03 '15 at 14:59
  • I will but think there's a delay before I can answer my own question. – Steve Crane Dec 03 '15 at 15:34

1 Answers1

3

Your error message complaining about Set-Location C:\Projects\Database command not found, but command being marked as error is db. The only reasonable explanation of this error message I can find, is that db command is an alias to Set-Location C:\Projects\Database. You possibly try to create an alias before trying to make function. Aliases have higher precedence than functions. So that, even you have db function now, db still resolved as an alias and produce error. You have to remove alias Remove-Item Alias::db, then db would be resolved to function.

user4003407
  • 21,204
  • 4
  • 50
  • 60
  • Yes, that is exactly what had happened. I initially tried creating an alias and had left it behind when I moved on to trying a function. – Steve Crane Dec 07 '15 at 13:02