10

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?

11

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 .

  • Is there any file where these aliases are stored? It would be of great use if I backup it. – Santosh Kumar Dec 8 '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 3: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 8:17
  • @SantoshKumar Your solution requires the '/d' flag to change DRIVE then directory || "cd /d D:\xampp\htdocs" – Eddie B Jan 24 '13 at 21:35
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
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.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.