10

I'm running a bunch of python scripts that are located in a couple of folders on my desktop. I hate having to type

'cd "C:/Path/to/desktop/folder"'

to go from one folder to another that contains scripts. I want to be able to switch folders easily like

'cd ..'
'folder 1'

so i can switch back to the desktop directory and the type in just the folders name. Is this possible with powershell? Do I just need to add the desktop path to the path environment, if so how do I do this in powershell

MattDMo
  • 100,794
  • 21
  • 241
  • 231
shreddish
  • 1,637
  • 8
  • 24
  • 38
  • As for your second question about adjusting environment variables from PowerShell, you can use the `$env:` environment provider like this: `$env:PATH += ";C:/Path/to/desktop/folder"` – Davor Josipovic Jun 20 '13 at 16:53

8 Answers8

19

cd ~/Desktop or cd ~\Desktop

is what you need.

georgeok
  • 5,321
  • 2
  • 39
  • 61
3

First : You've got the PowerShell pending to the subst.exe cmd.exe command line interpreter.

New-PSDrive -Name py -Root "cd C:/Path/to/desktop/folder" -PSProvider filesystem

then

cd py:

Second : as in linux you can use the location based CmdLets

push-location "C:/Path/to/desktop/folder"
get-location -stack
pop-location
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
2

Personally, I don't like using the New-PSDrive commandlet like this. Like you, I been looking for a better solution. Here is what I came up with.

Put in Powershell profile:

Function GoToFolder {cd "C:/Path/to/desktop/folder"}

Example use:

PS C:/Path/to> GoToFolder 
PS C:/Path/to/desktop/folder>

It may not be the most elegant solution and I am sure there are other ways, but it works very well.

Lucubrator
  • 1,098
  • 2
  • 10
  • 25
1

Depending on the version of PowerShell and how many different scripts you are running you could just use an alias for each script itself.

For Example:
Set-Alias "One" "C:/Path/to/desktop/folder/ScriptOne.ps1"
Set-Alias "Two" "C:/Path/to/other/desktop/folder/ScriptTwo.ps1"

That way you can just use the alias from anywhere (because they have the full path) and it will run that script without you having to cd all the time.

Schuyler
  • 509
  • 1
  • 9
  • 19
1

From your problem description, I derive either two things:

  1. You are not aware that you can use cd with relative paths
  2. You want the path to your desktop to be the default working directory for PowerShell

If your working directory is C:\Users\<username>\Desktop, then you can indeed just use cd folder1 to change the directory to C:\Users\<username>\Desktop\folder1 and use cd .. to change back without specifying any full absolute path.

If you are annoyed by the fact that C:\Users\<username>\Desktop is not the default working directory of PowerShell and you have to set this working directory on each new PowerShell window, then you can modify it like described in this question.

stackprotector
  • 10,498
  • 4
  • 35
  • 64
0

I just set variables in my profile...

$fdev = "D:\dev\blah\stuff\longstring\"

then use lika-so:

pushd $fdev
popd
Steve Goranson
  • 329
  • 2
  • 8
0

PS C:\Users\User> Links\Desktop.lnk if you want to go straight to desktop not closing windows one by one... (it will not open desktop like it is it will open it a bit in different way, but it will do)

-1

You should create an alias.

Set-Alias alias command

example

Set-Alias "folder" "cd C:/Path/to/desktop/folder"

Edit: if you want to use argument you should make a script (using or not an alias to call it)

elnabo
  • 264
  • 1
  • 2
  • 10
  • 1
    Try your Alias. This not the way to use Alias in PowerShell. – JPBlanc Jun 20 '13 at 18:21
  • Aliases do not work if you need to pass arguments (like passing `C:/Path/to/desktop/folder` to `cd`). To pass arguments, you have to define functions like in @Lucubrators answer. This is well documented in the [`Set-Alias`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-alias?view=powershell-7) documentation (see Example 5). – stackprotector Apr 21 '20 at 08:37