7

Is it possible in PowerShell to create alias for path?

For example: I have to write all time

PS PS C:\Users\Jacek>cd C:\windows\microsoft.net\framework\v4.0.30319

I will happy if I could write

PS PS C:\Users\Jacek>cd dotNet4path
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Jacek
  • 11,661
  • 23
  • 69
  • 123

4 Answers4

11

You could just create a variable in your powershell profile with that path as the value.

Then all you would need to do is something like

cd $dotNet4path

To add something to your profile profile, type $profile, into a powershell window and it will display the path to your powershell profile file. Create it if it does not exist.

Then put this line in the file, save and restart powershell.

$dotNet4path = "C:\windows\microsoft.net\framework\v4.0.30319"
DanL
  • 1,974
  • 14
  • 13
  • Is it permament solution? If I resrat my computer this alias will still exists? – Jacek Apr 27 '15 at 08:18
  • yes as long as your powershell profile file exists. It automatically gets run when you load a powershell window unless you use a command line switch to skip loading the profile – DanL Apr 27 '15 at 08:19
  • This is good, but what about just having the path in an alias. Such as `dotNet4Path` executes `cd C:\windows\microsoft.net\framework\v4.0.30319` no need `cd $dotNet4Path`. I know the question was not that, but I would really like to know it. – Agil Oct 10 '19 at 06:50
  • I tried to go Set-Alias but it didn't worth the effort after finding this simple solution. – Samiullah Khan Jul 15 '20 at 06:01
6

you could use an alias to execute a custom function (you may want to do this in your profile) or use a psdrive :

function path2dotNet{cd "C:\Users\Jacek>cd C:\windows\microsoft.net\framework\v4.0.30319"}  
Set-Alias dot4 -Value path2doc

New-PSDrive dotnet -PSProvider FileSystem -Root "C:\Users\Jacek>cd C:\windows\microsoft.net\framework\v4.0.30319" 
cd dotnet:
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
1

You could use your powershell profile to store the path in a variable:

Add-Content -Value '$dotNet4path = C:\windows\microsoft.net\framework\v4.0.30319' -Path $profile -Force

If you restart your powershell, you can type

cd $dotNet4path
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
0

Inspired by @Loïc MICHEL -

`Updated variant for PS 5.1:

set alias to long path using New-PSDrive

Using persist and defining -Root value as network shared path (on local system but appears 'remote') drive F: will persist across PS sessions

change < > entries to your UNC shared path (need to share it first, can select yourself as only authorized user)`

New-PSDrive -Name "F" -Persist -PSProvider "FileSystem" -Root "\\"

netify
  • 1
  • 2