21

By default my Windows PowerShell starts in C:\Users\Santosh, my XAMPP installation is in D:\ so the htdocs folder is located at D:\xampp\htdocs. If I have to edit something in htdocs folder then I have to type full cd D:\xampp\htdocs\ (autocompletion is not so kind) then edit that file.

If this PowerShell were a Bash I would do this in .bash_aliases file:

alias htdocs='cd D:\xampp\htdocs'

Is it possible to maintain Bash aliases like file and alias any command in PowerShell?

Santosh Kumar
  • 1,002
  • 4
  • 14
  • 35

5 Answers5

17

You want the set-alias commmand in coombination with a powershell script or a function. So open a editor and write:

set-location d:\xampp\htdocs

and save this file for example to c:\Users\kumar\htdocs32.ps1 or you can create a function like this.

function htdocs32 { set-location d:\xampp\htdocs }

to execute scripts you must set the execution policy allowing scripts locally. open the powershell command line as administrator and type:

set-executionpolicy remotesigned

now you are able to set an alias for the powershell script:

set-alias htdocs c:\Users\kumar\htdocs32.ps1

and typing htdocs now will cd you into your htdocs folder

Powershell is using a verb-noun combination for the naming of so called cmdlets. The verb referse to what you want to do and the noun with what you want to do something.

To get help for the set-alias command you want to use:

get-help set-alias -full  |more 

and no there is no less. the other method would be reading this http://technet.microsoft.com/en-us/library/ee176958.aspx

Also to start off with power shell i recommend you to have a look at this url: http://www.powershellpro.com/powershell-tutorial-introduction/

To save the alias permanently you must save it in your users profile. first test whether a Profile is already in place using:

PS C:\> $profile

if you getting false you can create a new profile by typing:

 New-Item -path $profile -type file -force

now you can edit the file

c:\Users\kumar\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

and put in the function definiton and an alias. as described above.

however setting an alias for this in linux is not neccessary. sicne there is a environmentvariable $CDPATH for bash which can be set in ~/.bahsrc .

l1zard
  • 1,064
  • 2
  • 10
  • 19
  • Is there any file where these aliases are stored? It would be of great use if I backup it. – Santosh Kumar Dec 08 '12 at 17:01
  • Getting `Cannot resolve alias 'htdocs' because it refers to term 'cd D:\xampp\htdocs', which is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.` on doing `set-alias htdocs "cd D:\xampp\htdocs"` – Santosh Kumar Dec 10 '12 at 03:26
  • ok you better write a ckdlet. open a texteditor and write: set-locaation c:\xampp\htdocs and save this file as htdocs32.ps1. assuming htdocs32.ps1 was saved in c:\Users\kumar\htdocs32.ps1 you can use set-alias htdocs c:\Users\kumars\htdocs32.ps1 . make sure you have set you execution policy accoringly. – l1zard Dec 10 '12 at 08:17
  • @SantoshKumar Your solution requires the '/d' flag to change DRIVE then directory || "cd /d D:\xampp\htdocs" – Edward J Beckett Jan 24 '13 at 21:35
2

Sometimes a function works better than an alias in PowerShell. Unlike the bash alias, Windows' Set-Alias expects single-word assignment, not a command string built up of several elements.

PS > function cal { bash -c cal }

PS > cal

     April 2023
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
MarkHu
  • 721
  • 1
  • 6
  • 11
2

Actually you could try this, it will create a module loaded automatically.

Under C:\Users\kumar\Documents\WindowsPowerShell\

Create a new folder Modules,if not existing.

PS C:\>mkdir Modules

Under Modules create a folder call ex:"Quicky"

PS C:\>mkdir Quicky

Create a file called "quicky.psm1", .psm1 is the extension for Modules.

Edit the file and add that line.

function htdocs32 { set-location d:\xampp\htdocs }

Save the module.

Then simply call the function "htdocs32"

PS C:\>htdocs32
DavidRG
  • 21
  • 2
0

if you have not-too-restricted powershell script execution policy, you can just a script to do it. Remember, unlike BASH, a Windows script can affect your shell after completion so you don't need an alias or bash function equivalent.

htdocs.ps1 (put this somewhere on your PATH, I'd go with cdhtdocs.ps1 myself, but it's your naming convention)

chdir "d:\xampp\htdocs"

Note: this will work to change drives as required too. i.e. it would work if you were in c:\temp\ to start with.

JL Peyret
  • 772
  • 3
  • 15
  • 25
0

I was struggling with this for a while and I've written this PowerShell module:

https://www.powershellgallery.com/packages/HackF5.ProfileAlias

https://github.com/hackf5/powershell-profile-alias

To get started you install it by:

Install-Module HackF5.ProfileAlias
Register-ProfileAliasInProfile

Then use it like:

Set-ProfileAlias dkfeed 'Enter-Docker feed_app_container' -Bash

I've been using it for a while myself and I've found it fairly useful.

(It only runs on PS 7.0 as I wrote it for myself).

satnhak
  • 101
  • 3
  • 1
    Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by [voting to close it as a duplicate](https://superuser.com/help/privileges/close-questions) or, if you don't have enough reputation for that, [raise a flag](https://superuser.com/help/privileges/flag-posts) to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places. – DavidPostill Feb 05 '21 at 19:28
  • 1
    Sorry dude, I've deleted the other post. I spent a bit of time on this one because it's a great feature in bash that I really missed. My only intention was to give other people a chance to try it out to see if it solved some of their problems. – satnhak Feb 11 '21 at 16:42
  • This brief answer doesn't do justice to this awesome PowerShell module. Check out the https://github.com/hackf5/powershell-profile-alias/blob/master/README.md which goes more in-depth. – MarkHu Aug 11 '23 at 20:35