What is the alternative for ~ (user's home directory) on Windows command prompt?
Asked Answered
G

13

373

I'm trying to use the command prompt to move some files, I am used to the linux terminal where I use ~ to specify the my home directory I've looked everywhere but I couldn't seem to find it for windows command prompt (Documents and Settings\[user])

Griffin answered 10/2, 2012 at 14:4 Comment(1)
Here's a list of windows environment variables: learn.microsoft.com/en-us/windows/deployment/usmt/…Baiss
E
555

You're going to be disappointed: %userprofile%

You can use other terminals, though. Powershell, which I believe you can get on XP and later (and comes preinstalled with Win7), allows you to use ~ for home directory.

Equable answered 10/2, 2012 at 14:8 Comment(6)
You can find complete list of environment variables here.Aloisia
>> Powershell ... allows you to use ~ -> but not, ironically %userprofile%: Set-Location: Cannot find path 'C:\%userprofile%' because it does not exist.Batiste
@mcalex: In PS, you usually refer to environment variables like this: $env:userprofile. cd $env:userprofile works just fine. Besides that, C:\%userprofile% will always be an invalid path, as the environmnet variables already includes the drive letter.Idiophone
I think C:\%userprofile% was what PS attempted to access, from a working directory of C:\, not what @Batiste typed.Walz
%USERPROFILE% is equivalent to %HOMEDRIVE%%HOMEPATH% it looks like. I tested this and saw it to be true too.Saltatorial
it would be really useful if we could use ~ in Explorer and environment variables as well. Much easier to rememberBeholden
A
69

You can %HOMEDRIVE%%HOMEPATH% for the drive + \docs settings\username or \users\username.

Alkylation answered 10/2, 2012 at 14:11 Comment(2)
This answer worked for a service running as local system account. %userprofile% in this case does not.Izmir
Worth noting that this might give an unexpected result on machines with networked filesystems, e.g. corporate machines. On mine, %userprofile% is C:\Users\me, but %homedrive%%homepath% points to the root of a mapped `U:` drive.Spitsbergen
V
42

You can use %homedrive%%homepath% environment variable to accomplish this.

The two command variables when concatenated gives you the desired user's home directory path as below:

  1. Running echo %homedrive% on command prompt gives:

    C:
    
  2. Running echo %homepath% on command prompt gives:

    \Users\<CurrentUserName>
    

When used together %homedrive%%homepath% it gives:

C:\Users\<CurrentUserName>
Vacillation answered 23/3, 2017 at 2:19 Comment(7)
you'll also need %homedrive%; see Alex K's answer, aboveMaharashtra
@EdwardFalk I've fixed my answer. I had missed to mention %systemdrive% environment variable which is equivalent of %homedrive% mentioned by Alex. Please do give me your feedback on this if you find anything incorrect.Vacillation
In windows you can move your entire user folder to another drive (Ex: D:\MyUser). In those scenarios %systemdrive%%homepath% will fail. %HOMEDRIVE%%HOMEPATH% should be used instead.Pharisaic
True. It is possible but it isn't that usual for people to change these standard environment variables. In a usual corporate environment these environment variables are governed by IT department through AD group policies as mentioned here and here.Vacillation
So in the common case, they give the same answer. In the uncommon case, only %HOMEDRIVE%%HOMEPATH% is the correct answer. Therefore %HOMEDRIVE%%HOMEPATH% is the correct answer in all cases, and I recommend changing your answer accordingly.Fantinlatour
I don't see how this answer adds anything over Alex's.Bacchanalia
Looking at the history of the post edits will help you understand it. Initially my answer was %systemdrive%%homepath% but later on got modified by community.Vacillation
R
13

Update - better version 18th July 2019.

Final summary, even though I've moved on to powershell for most windows console work anyway, but I decided to wrap this old cmd issue up, I had to get on a cmd console today, and the lack of this feature really struck me. This one finally works with spaces as well, where my previous answer would fail.

In addition, this one now is also able to use ~ as a prefix for other home sub-folders too, and it swaps forward-slashes to back-slashes as well. So here it is;

Step 1. Create these doskey macros, somewhere they get picked up every time cmd starts up.

DOSKEY cd=cdtilde.bat $* 
DOSKEY cd~=chdir /D "%USERPROFILE%"
DOSKEY cd..=chdir ..

Step 2. Create the cdtilde.bat file and put it somewhere in your PATH

@echo off

set dirname=""
set dirname=%*
set orig_dirname=%*

:: remove quotes - will re-attach later.
set dirname=%dirname:\"=%
set dirname=%dirname:/"=%
set dirname=%dirname:"=%

:: restore dirnames that contained only "/"
if "%dirname%"=="" set dirname=%orig_dirname:"=%

:: strip trailing slash, if longer than 3
if defined dirname if NOT "%dirname:~3%"==""  (
    if "%dirname:~-1%"=="\" set dirname="%dirname:~0,-1%"
    if "%dirname:~-1%"=="/" set dirname="%dirname:~0,-1%"
)

set dirname=%dirname:"=%

:: if starts with ~, then replace ~ with userprofile path
if %dirname:~0,1%==~ (
    set dirname="%USERPROFILE%%dirname:~1%"
)
set dirname=%dirname:"=%

:: replace forward-slashes with back-slashes
set dirname="%dirname:/=\%"
set dirname=%dirname:"=%

chdir /D "%dirname%"

Tested fine with;

cd ~ (traditional habit)
cd~  (shorthand version)
cd.. (shorthand for going up..)
cd / (eg, root of C:)
cd ~/.config (eg, the .config folder under my home folder)
cd /Program Files (eg, "C:\Program Files")
cd C:/Program Files (eg, "C:\Program Files")
cd \Program Files (eg, "C:\Program Files")
cd C:\Program Files (eg, "C:\Program Files")
cd "C:\Program Files (eg, "C:\Program Files")
cd "C:\Program Files" (eg, "C:\Program Files")

Oh, also it allows lazy quoting, which I found useful, even when spaces are in the folder path names, since it wraps all of the arguments as if it was one long string. Which means just an initial quote also works, or completely without quotes also works.

All other stuff below may be ignored now, it is left for historical reasons - so I dont make the same mistakes again


old update 19th Oct 2018.
In case anyone else tried my approach, my original answer below didn't handle spaces, eg, the following failed.

> cd "c:\Program Files"
Files""]==["~"] was unexpected at this time.

I think there must be a way to solve that. Will post again if I can improve my answer. (see above, I finally got it all working the way I wanted it to.)


My Original Answer, still needed work... 7th Oct 2018.
I was just trying to do it today, and I think I got it, this is what I think works well;

First, some doskey macros;

DOSKEY cd=cdtilde.bat $* 
DOSKEY cd~=chdir /D "%USERPROFILE%"
DOSKEY cd..=chdir ..

and then then a bat file in my path;

cdtilde.bat

@echo off
if ["%1"]==["~"] ( 
    chdir /D "%USERPROFILE%"
) else ( 
    chdir /D %* 
)

All these seem to work fine;

cd ~ (traditional habit)
cd~  (shorthand version)
cd.. (shorthand for going up..)
Rosio answered 7/10, 2018 at 12:48 Comment(3)
This answer has a way to start CMD with a .bashrc-type file: superuser.com/questions/144347/…. Putting step 1. from this answer into the bashrc.bat file will set it at startup.Wendeline
Just to clarify; although bash follows similar pattern as well, so it is "bash-like" there is no requirement for bash to be used in order to achieve this. I simply use a very old DOS trick, with windows registry. I use the windows registry location "HKCU\Software\Microsoft\Command Processor", find the string key/value "Autorun" and enter the filename of a cmd file of your choice. eg, I have this: "C:\Users\username\.autorun.cmd" you can put this file in any location that makes sense to your configuration. Windows will run this file every time you open up the dos command prompt.Rosio
@Wendeline you provided a good link, yes that is how I achieved it, without bash, but bash-likeRosio
R
4

I just tried set ~=%userprofile% and that works too if you want to keep using the same habit

You can then use %~% instead.

Rectangle answered 24/10, 2016 at 20:11 Comment(3)
How do you exactly set the value of ~ = %userprofile%? I ran the exact command mentioned in your post on command prompt but it throws error - '~' is not recognized as an internal or external command, operable program or batch file.. Can you please elaborate your answer bit more?Vacillation
If you use set ~=%userprofile% then you can use %~% for the variableFinality
@Vacillation if you're using powershell, try ${env:~}="$env:homedrive$env:homepath".Fantinlatour
H
4
# cmd
# use:  %USERPROFILE%

# example:
kubectl config --kubeconfig=%USERPROFILE%\.kube\config.xyz view
# powershell
# use:  $env:USERPROFILE

# example
kubectl config --kubeconfig=$env:USERPROFILE\.kube\config.xyz view
Hua answered 13/8, 2021 at 1:59 Comment(0)
A
2

If you want a shorter version of Jay's you could try

    set usr=%userprofile%
    cd %usr%

Or you could even use %u% if you wanted to. It saves some keystrokes anyway.

Admixture answered 10/1, 2016 at 23:13 Comment(0)
P
2

You can do almost the same yourself. Open Environment Variables and click "New" Button in the "User Variables for ..." .
Variable Name: ~
Variable Value: Click "Browse Directory..." button and choose a directory which you want.

And after this, open cmd and type this:
cd %~%
. It works.

Paterson answered 9/5, 2017 at 6:38 Comment(0)
H
2

Simply

First Define Path

doskey ~=cd %homepath%

Then Access

~
Headland answered 22/10, 2019 at 13:45 Comment(0)
I
1

Use %systemdrive%%homepath%. %systemdrive% gives drive character ( Mostly C: ) and %homepath% gives user home directory ( \Users\<USERNAME> ).

Inanimate answered 1/1, 2021 at 12:3 Comment(0)
S
0

Just wrote a script to do this without too much typing while maintaining portability as setting ~ to be %userprofile% needs a manual setup on each Windows PC while cloning and setting the directory as part of the PATH is mechanical.

https://github.com/yxliang01/Snippets/blob/master/windows/

Scrawny answered 24/3, 2017 at 20:1 Comment(0)
A
0

cd ~ will work fine on windows powershell, if you experience unsupported commands make sure you are using powershell instead of CMD or use windows GIT bash.

Actualize answered 19/9, 2022 at 15:0 Comment(0)
A
-7

You can also do cd ......\ as many times as there are folders that takes you to home directory. For example, if you are in cd:\windows\syatem32, then cd ....\ takes you to the home, that is c:\

Analgesic answered 21/9, 2018 at 5:24 Comment(2)
home != c:\ Instead, Original Poster asks about C:\Users\username folder (or c:\Documents and Settings\username in earlier Windows)Workingman
Even if you wanted to go to the root C:\ it is rather advised to use cd \ instead of an undefined number of cd ..\..\..Qualls

© 2022 - 2024 — McMap. All rights reserved.