22

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,062
  • 4
  • 14
  • 36

5 Answers5

18

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,084
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
  • 741
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
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
  • 884
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