104

In Linux, we can go to the user home by entering the tilde (~) character after cd:

cd ~

How to do the same in Windows?

Each time, I need to type:

cd C:\Document and Settings\freewind

That's too boring.

slhck
  • 228,104
Freewind
  • 1,897
  • 2
    Consider using cygwin ? – Paul R Sep 07 '11 at 12:29
  • 8
    actually, '~' is the default for 'cd' under most Linux shells, so just 'cd' would suffice to go to your homedir –  Sep 07 '11 at 12:44
  • @Arnout: See http://stackoverflow.com/questions/998626/meaning-of-tilde-in-linux-bash-not-home-directory – abatishchev Sep 07 '11 at 13:11
  • 4
    The answer is simply "cd ~".. means, you can use the same command in Windows too. But, you shouldn't try this command in CMD because its out-dated now & its development is discontinued by Microsoft. Use "Windows PowerShell" and the same command will work smoothly. – user79032 Sep 14 '11 at 04:03
  • @SachinShekhar Unless I'm mistaken, PowerShell doesn't even ship with Windows. If he has to install an application, he may as well just install cygwin. – weberc2 Jan 16 '13 at 21:09
  • 3
    PowerShell ships with Win7+ – Isaac Dontje Lindell Jan 16 '13 at 21:11
  • @weberc2 Powershell is included in all Windows since Windows 7 – phuclv Apr 03 '16 at 09:59
  • @weberc2 PowerShell is now the default on Windows 10. – wjandrea Sep 30 '17 at 04:58
  • It's a feature of shells, not of the kernel, so the "equivalent of Linux's ~ (tilde) in Windows" is exactly the tilde, you just need to use a shell that supports it. – Kamil Maciorowski Jan 03 '24 at 05:15

9 Answers9

75

You can use cd /d %USERPROFILE% if you use cmd.

or you can use cd ~ if you use PowerShell.

Nocturn
  • 851
  • 5
    +1 but also add quotes around the variable (maybe it's c:\documents and settings\..., the spaces will break it unless it's quoted) – Jon Sep 07 '11 at 12:47
  • 9
    @Jon: cd is an exception. Since it always takes a single argument, quoting is not necessary. (On Windows, programs and built-ins must parse their command line themselves, the shell does not do it.) Always quoting paths is a good practice, however. – u1686_grawity Sep 07 '11 at 14:45
  • 1
    @barlop: usually CommandLineToArgvW(); the CLR/MinGW/Cygwin runtimes also use their own equivalent automatically before calling main(). Some programs, however, have parsers written entirely from scratch, for example, cmd.exe itself and its wacky /c. – u1686_grawity Nov 08 '11 at 23:18
  • @grawity well, I don't see why one would call it wacky 'cos cmd /? shows that /c or /k is the last switch to use, and whatever follows /c or /k is a string. So i'd expect what follows the /c or /k to be combined but not with that which precedes it, the /c or /k. Putting quotes in for that which is after /c or /k would be superfluous. – barlop Nov 09 '11 at 02:25
  • 1
    @barlop: Not if you're trying to use quotes (or redirection) as part of the given command. Is it /c "C:\foo bar\baz" "my file.txt" or /c "\"C:\foo bar\baz\" \"my file.txt\"" or /c ""C:\foo bar\baz" "my file.txt""? What's even worse is that the parsing depends on whether /s was specified earlier, the presence of special characters, whether the executable is named C:\foo bar\baz or just C:\foo... – u1686_grawity Nov 11 '11 at 10:47
  • 1
    @TheLQ A batch script is even easier still. I can accomplish the same result by typing a single character. See my answer below for a description. – weberc2 Jan 16 '13 at 21:05
  • ::{59031a47-3f72-44a7-89c5-5595fe6b30ee} in windows explorer is the same as pasting %USERPROFILE% – Mark Schultheiss Aug 10 '16 at 19:55
62

cd /d "%HOMEDRIVE%%HOMEPATH%" would do it -- but I 'm not sure if you consider it an improvement.

You can also define an alias for the above command:

doskey cdhome=cd /d "%HOMEDRIVE%%HOMEPATH%"

After this, it's simply cdhome.

Jon
  • 1,081
  • 5
    @Freewind: You gotta love the default Windows shell. – Jon Sep 07 '11 at 12:33
  • 1
    Just wondering what is the difference between %USERPROFILE% and %HOMEDRIVE%%HOMEPATH% – abatishchev Sep 07 '11 at 12:41
  • 12
    @abatishchev: Good question. This question on SF seems to indicate that %USERPROFILE% is preferable. – Jon Sep 07 '11 at 12:50
  • 5
    You can also use cd~ as the macro name, instead if cdhome. – jftuga Sep 07 '11 at 14:16
  • 1
    @Jon, I found when I close the shell, the doskey won't work anymore. How to make it always available? – Freewind Sep 07 '11 at 14:19
  • 1
    @Freewind: You will need to set it up to execute whenever a shell is started, which can be accomplished using the registry. – Jon Sep 07 '11 at 14:30
  • 4
    @abatishchev: Windows NT accounts can have a "home directory" path attached which is separate from the "profile directory". The profile keeps your settings (Registry), application data, and such. The home directory, on the other hand, is for your own files -- when a program displays the "Open/Save" dialog for the first time, it will start at the home directory. (Windows 95/98 used it somewhat differently; can't remember the specifics.) This is useful in Active Directory environments, for storing files on a network share but keeping the profile local. – u1686_grawity Sep 07 '11 at 14:41
  • 3
    @jftuga: Or even ~. – u1686_grawity Sep 07 '11 at 14:43
  • 1
    Why not just create a batch script and place it in your path? ~.bat could contain cd %USERPROFILE%, and it would be accessible by typing ~ at your command prompt. Furthermore, it would be valid outside of your shell without registry hacks. – weberc2 Jan 16 '13 at 20:56
  • @user1686 and how exactly do I separate those directories? or is it not supported in later OS versions? – Unknow0059 Oct 20 '20 at 17:39
  • 1
    @Unknow0059: Look for the "Profile" tab in dsa.msc for AD accounts, lusrmgr.msc for local accounts. – u1686_grawity Oct 20 '20 at 18:47
  • For posteriority: The profile tab can be found after clicking and opening the Properties of an entry in the results list of lusrmgr. – Unknow0059 Oct 21 '20 at 05:01
13

One possibility is to use the subst command from a command prompt:

subst z: C:\Document and Settings\freewind

Any time you navigate to drive Z:, you'll be looking at your user folder.

The downside is that you need to run it every time you log in. I used a batch file and just put it in my startup folder, but there are probably more elegant solutions to this.

The benefit is that unlike a doskey alias, it works universally (windows explorer, browse dialog, etc.), not just when changing directories at the command prompt. It's especially helpful for old programs with old browse dialogs that have drive letters at the topmost level, rather than "desktop".

mo.
  • 238
  • You can map a drive and make it automatically remap after login. No need to use subst – phuclv Jun 29 '15 at 08:32
  • 1
    @LưuVĩnhPhúc It may not be entirely logical, but I tend to avoid windows network share things unless I really have to use them. I feel like performance and reliability is... unpredictable? By all means if you have an alternative, nay better, way of doing it, please post it in its own answer for the benefit of everyone :) – mo. Jun 29 '15 at 13:38
  • No need for more software utilities. Mapping is the most efficient option. – Full Array Jan 03 '24 at 03:25
7

You can do this:

cd %homepath%
Bali C
  • 1,692
5

You can create a batch file called ~.bat containing

@echo off
cd /d %USERPROFILE%

And add it to your PATH variable (Start->[right-click on "Computer"]->Properties->"Advanced System Settings"[in left column]->"Environment Variables"

From there, just edit your PATH variable to include the folder containing your ~.bat file. (This allows you to invoke your script from anywhere using just the filename--not the absolute path to the file)

Also, make sure your PATHEXT environment variable contains .BAT somewhere (this allows you to type ~ instead of ~.bat).

When you need to use it, simply enter ~ at the command prompt. This solution is persistent--you won't have to set it up every time you launch a shell, and you won't need to hack your registry.

weberc2
  • 428
  • 3
  • 11
4

Unfortunately, I'm unaware of a perfect solution but there are a couple of hacky options:

Option 1: Set ~ to be a command alias with doskey

doskey ~=cd /d %USERPROFILE%

This will enable you to simply type ~ and have it chdir to your homedir.

C:\>~
C:\Users\a>

Obviously, this shortens the number of keystrokes to get home (even compared to Linux), but is less robust since you can't really use it as part of another path or do much with it beyond just cd'ing home.

Option 2: Set ~ to be a variable

Another option, if you want a more robust option is to set a var:

set ~="%USERPROFILE%"

but using this would be used like:

E:\>cd /d %~%
C:\Users\a>

It can also be injected into most paths and doesn't limit you to simply cd'ing home.

Both options simultaneously

Also, you can implement both methods simultaneously. If you have the %'s around it, then it's going to utilize the variable - otherwise it will treat ~ as a command.

C:\>:: ECHO (display) THE VALUE OF %~%
C:\>echo %~%
C:\Users\a

C:\>:: ChDir to ~\DESKTOP
C:\>cd /d %~%\Desktop

C:\Users\a\Desktop>:: USE THE ~ COMMAND
C:\Users\a\Desktop>~

C:\Users\a>

One other tip (kinda related):

This is less likely to be acceptable on a corporate machine, at least on your home PC, to save additional keystrokes, it's not a bad idea to make your %USERPROFILE% dir a simple name. As you can see from my examples above, my user dir is C:\Users\a - I used to always have 'aplocher' or 'adamp' and when I changed to a single letter, it was magical.

2

In Powershell the %variable% syntax does not work. You can do this :

cd $env:USERPROFILE
Hey
  • 1,261
0

In cmd.exe, you can make ~ work just like ~ in Unix systems with help of clink and its Lua scripts:

  1. Install latest version of clink.
  2. Download tilde_autoexpand.lua and place it in some directory, let's say:
C:\clink\Scripts\tilde-autoexpansion
  1. Install scripts from that directory with:
clink installscripts C:\clink\Scripts\tilde-autoexpansion
  1. Run clink set tilde.autoexpand true to enable it.

After that, ~ should automatically expand to home directory. Test it with e.g. cd ~. Restart cmd to reload clink if it doesn't.

Also note that by default to type regular tilde you need to use ~~. You can change this with tilde.escape setting.

Destroy666
  • 6,771
-1

Using PowerShell, you have two options.

  1. (Option 1) You may try the following method to navigate directories from anywhere provided you know your username and have file directory permissions granted.
cd \..\Users\yourUserName\Documents
cd \..\Users\yourUserName\Downloads
cd \..\Users\yourUserName\Desktop
cd \..\Users\yourUserName\music

See example below in PowerShell (64bit). I tested this on a Windows 7 Pro 64bit box.

Linux Lilde equivalent on Windows 7 Pro 64 Bit

  1. (Option 2) You can use the tilde character in powershell.
cd ~

Alternatively, you may install unix-like distribution emulators for Windows such as cygwin, so you get unix-like environment functionality while using command prompt (CMD) on a Windows OS.