Is there a shortcut command in Windows command prompt to get to the current user's home directory like there is in Linux?

110

19

I am used to using

cd ~

to get right into my home directory. In Windows command prompt I have to do

cd Users\username

to get there. Is there a shortcut like the Linux one? It would be nice if I could get there by doing

cd username

Is something like this possible in Windows Vista?

classer

Posted 2010-07-28T09:47:26.973

Reputation: 2 465

7In Unix, there's no need for the tilde. cd with no arguments will change to your home directory. – coneslayer – 2010-07-28T12:08:59.393

there was a question like this here on su, can't find it. – akira – 2010-07-28T12:59:07.153

1@coneslayer i know but to move a file from one directory to another you use the tilde. – classer – 2010-08-01T08:21:17.703

As simple as cd %userprofile% – subtleseeker – 2019-06-08T17:16:58.470

You can use Windows Power Shell (on Win XP and later) which allows you to use ~ for the home directory. See also ----> https://stackoverflow.com/questions/9228950/what-is-the-alternative-for-users-home-directory-on-windows-command-prompt

– franz1 – 2019-10-04T09:53:25.217

Answers

19

You can always put a .bat-File somewhere in your %PATH% which does the path changing for you.

dertoni

Posted 2010-07-28T09:47:26.973

Reputation: 1 099

167

Yes, you can use %HOMEPATH%, which is the full path of the user's home directory.

There are quite a few other useful variables available, such as %OS% (Operating System), or %WINDIR% (Windows system directory). See Wikipedia: Environment Variables for a list.


Notes:

Actually, things are a bit complicated (as usual). %HOMEPATH% only contains the path, without the drive letter, so will not work for cding from a different drive. You can also use %USERPROFILE%, which does contain the drive letter, and is usually the same directory as %HOMEPATH%.

The values of these variables, and which one is right for you, will depend on the Windows version and any changes by an administrator, as their values may be different (see e.g. the question Difference between profile and home path ).

sleske

Posted 2010-07-28T09:47:26.973

Reputation: 19 887

This saved my life! After looking at a lot of other answers from different questions. Again! This worked for me and should be the marked as the answer for this question. – Patricia – 2016-01-25T06:43:42.590

@MissLucy: I'm happy it helped you. As to accepting an answer: That's up to the OP to decide. You could add a comment to the question to alert them. – sleske – 2016-01-25T09:39:14.477

1use %USERPROFILE% instead. – JJS – 2016-04-08T19:43:17.467

On my Windows 10 1809 %HOMEPATH% contains just a root directory \. %USERPROFILE% is OK. – pabouk – 2019-09-24T14:47:09.850

@pabouk: On my system (also Windows 10 V 1809), %HOMEPATH% works as described in the answer (full path, without drive letter). Maybe there is something unusual about your user profile/settings? – sleske – 2019-11-22T11:22:42.140

@sleske Yes, it looks like it is enforced from the company's domain. The worse part is that the path created from %HOMEDRIVE% (which you omitted in your answer) and %HOMEPATH% does not exist on my system. I will ask our technical support but this misconfiguration does not seem to seriously break anything. – pabouk – 2019-11-22T15:09:59.860

5This is the most correct answer, in my opinion, because it does not rely on hard-coded paths and any wacky filesystem links that may be between them. – Ed Orsi – 2014-03-11T16:57:03.127

3This is the correct answer. – Frederik Krautwald – 2014-06-03T22:30:17.350

23

Two other options both of which can be added to a script and auto-executed in a similar manner to BillP3rd's answer.

It's two more characters but...

SET ~=%HOMEPATH%    
CD %~%

or...

CD %~%\Desktop

Or...

doskey ~=cd %homepath%
~

Of course you can't use this ~ in paths but as a quick "jump to my home dir" typing ~ Enter is pretty quick.

Carl

Posted 2010-07-28T09:47:26.973

Reputation: 231

1Are you kidding me with the doskey thing??? I now have a single char shortcut to any folder I feel like cd into. This should be #1 answer. – Andrew – 2019-05-16T12:42:31.377

9

I created a .cmd file on a directory in my path, and named it "cd~.cmd". Its contents are:

@cd %HOMEPATH%

So I can type "cd~" from anywhere to get to my home directory. Not the same as "cd ~" (note the missing space) but close enough for me.

Jaime de los Hoyos M.

Posted 2010-07-28T09:47:26.973

Reputation: 190

2

Most clever answer. Combining yours with this answer by Kenny Evitt gives the best solution.

– Sнаđошƒаӽ – 2016-03-04T15:09:55.397

7

sleske's answer is almost exactly right, but it doesn't always work.

If your home directory is on a network share setup as a mapped drive, run the following regardless of the drive of the current directory:

cd /D %HOMEDRIVE%%HOMEPATH%

The /D switch is necessary to allow cd to change drives.

Kenny Evitt

Posted 2010-07-28T09:47:26.973

Reputation: 273

5

Is there a shortcut for C:\Users\<current user>\?

There is no direct shortcut.

There are a couple of different solutions (see below).

  1. Use an environment variable together with cd or cd /d

  2. Use subst or net use to creating a mapping to another drive letter.

  3. Install cygwin and use bash

  4. Use powershell - powershell supports ~

The last solution is probably the simplest if you are prepared to use powershell instead of cmd.


Solution 1: Use an environment variable together with cd or cd /d

If you want to change to this directory on a regular basis then run the following command:

setx DOCS %USERPROFILE%

This will permanently set the environment variable DOCS, but in order to use use it you need to first start a new cmd shell, then the variable is defined and ready to use:

F:\test>echo %DOCS%
C:\Users\DavidPostill\

To change directory from any location use the following command:

cd /d %DOCS%

If you are already on drive c: you can just use:

cd %DOCS%

Create a batch file (docs.cmd) and put it somewhere in your PATH.

docs.cmd:

@echo off
cd /d %DOCS%

You can then just type docs regardless of your current location and it will take you to C:\Users\<current user>


Solution 2: Use subst or net use to creating a mapping to another drive letter.

You can use subst:

subst x: %USERPROFILE%

And then

x:

Unfortunately drive mappings do not persist across reboots.

net use will persist across reboots, for example:

net use x: "\\computerName\c$\pathName" /persistent:yes

See the answers in How to make SUBST mapping persistent across reboots? for detailed instructions.


Solution 3: Install cygwin and use bash

You could consider installing cygwin:

Cygwin is:

  • a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows.

Once you have installed cygwin you can run bash in a cygwin terminal and set the bash environment variable HOME as appropriate.

Alternatives to cygwin include msys (MingW):

MSYS is a collection of GNU utilities such as bash, make, gawk and grep to allow building of applications and programs which depend on traditionally UNIX tools to be present. It is intended to supplement MinGW and the deficiencies of the cmd shell.

And Git for Windows:

Git for Windows provides a BASH emulation used to run Git from the command line. *NIX users should feel right at home, as the BASH emulation behaves just like the "git" command in LINUX and UNIX environments.


Solution 4: Use powershell

As pointed out in a comment on another question by SBI powershell supports ~ and you can just type:

cd ~

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • cd - Change Directory - Select a Folder (and drive)
  • setx - Set environment variables permanently, SETX can be used to set Environment Variables for the machine (HKLM) or currently logged on user (HKCU).
  • subst - Substitute a drive letter for a network or local path.

DavidPostill

Posted 2010-07-28T09:47:26.973

Reputation: 118 938

If on Powershell there are two additional ways:

cd $HOME and cd $env:HOMEPATH – Dirk Thannhäuser – 2018-05-31T21:04:00.900

3

Dunno if its a feature of our work login script or a windows default, but I can use cd %HOMEPATH% to achieve that, where HOMEPATH is an environment variable.

Rob Cowell

Posted 2010-07-28T09:47:26.973

Reputation: 1 273

I see. It still takes a lot to write %HOMEPATH%. Also you can't auto-complete it. Is there a way I can make my username be equal to %HOMEPATH% by making a new command in cmd.exe? – classer – 2010-07-28T12:03:10.623

1

If you want all user's commmand prompts to start in their "home" directory, create the following registry key as an Expandable String Value (sans quotes, of course):

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun : "cd /d %USERPROFILE%"

If you want only your command prompts to do it:

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun : "cd /d %USERPROFILE%"

I make it a practice to keep a c:\Scripts folder in which I keep an "autoexec"-type batch file which I invoke via this key.

BillP3rd

Posted 2010-07-28T09:47:26.973

Reputation: 5 353

Watch out, this will break bower and npm (and Visual Studio)... – Quandary – 2017-10-10T09:50:48.167

1

I realize this is a long since done question, but just for the record. Install clink, this extends your command prompt in so many ways. Yes it it heavier then the above solutions but it makes the cmd window behave so much better.

THBBFT

Posted 2010-07-28T09:47:26.973

Reputation: 107

clink == powerful Bash-style command line editing for cmd.exe – suspectus – 2015-02-20T14:29:56.137

Right, and the whole point is that ~ resolves to the User{user} directory. – THBBFT – 2015-02-20T14:55:09.637

0

I used a better terminal (cmder) for this purpose. It has built in aliases and its very easy to use. Just read the documentation about Aliases here.

Don Camillo

Posted 2010-07-28T09:47:26.973

Reputation: 1

0

Windows has really become 'All about the gui' so in your case I'd just get the tools you want instead of trying to 'bend' the system to your will ... The MinGW tools are an excellent little collection of some of the most widely used gnu tools ... I highly recommend it if your a nix fan on Win ...

http://www.mingw.org/wiki/MSYS

Eddie B

Posted 2010-07-28T09:47:26.973

Reputation: 909