bash aliases equivalent for powershell?

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?


Solution 1:

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 .

Solution 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