I know about the %USERPROFILE%
system defined environment variable on Windows XP (and Vista and Windows 7). Is there a system defined environment variable pointing to the location of the "My Documents" directory? On XP by default it's %USERPROFILE%\My Documents
and on Win 7 it's %USERPROFILE%\Documents
. I just wanted to avoid having to test for the OS version in a Powershell script if I can avoid it.

- 1,764
- 5
- 21
- 33

- 14,928
- 14
- 81
- 132
-
7As one answer points out, there is no Environment Variable pointing to My Documents but there is `Environment.GetFolderPath(Environment.SpecialFolder.Mydocuments)` (C#) for .NET. I'm mainly adding this comment since this question comes up when googling for C#, environment variables and my documents and the easiest solution in .NET is using the above method. – flindeberg Oct 17 '12 at 16:22
-
You should have posted that as an answer @flindeberg; I'd have voted it up. Good to know. – Onorio Catenacci Oct 17 '12 at 20:09
-
I've added it as an answer now :) – flindeberg Oct 18 '12 at 07:53
-
1In windows there is a `junction` that links `documents` to `my documents`. `
My Documents [C:\Users\Philip\Documents]` – Marichyasana Feb 14 '16 at 12:18 -
Just a note: on Windows 10 you can move My Documents anywhere. I have it on drive D:, so mine is 'D:\Documents', so one cannot rely on '%USERPROFILE%\Documents' to work any more. – Neil Roy Apr 24 '23 at 18:20
16 Answers
For powershell the following works:
[environment]::getfolderpath("mydocuments")
and avoiding magic strings
[Environment]::GetFolderPath([Environment+SpecialFolder]::MyDocuments)
For .NET the following holds true (ie not applicable in all windows applications):
As one answer points out, there is no Environment Variable pointing to My Documents but there is Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
(C#) for .NET.
I'm adding this answer since this question comes up when googling for C#, environment variables and my documents and Justin's answer does not contain the line of code :)
Using the above mentioned line of code is the preferred way of accessing my documents in .NET :)
Copy paste this row for C# usage:
var directoryNameOfMyDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Note that C# needs a capital D in MyDocuments.

- 4,887
- 1
- 24
- 37
-
-
@mikemaccana Wow man, did you read what I wrote in this SIX year old answer? For avoidance of doubt: _I'm adding this answer since this question comes up when googling for C#, environment variables and my documents_ – flindeberg Oct 09 '18 at 14:24
-
@flinderberg Yes I did. This would still be better as a comment, or if you wanted to ask/answer your own question. – mikemaccana Oct 09 '18 at 16:25
On my default-installation XP system, there is no environment variable for that. You can list all variables with the "set" command ( no parameters ) in the command line. So probably you have to do a test.
If you don't want to test for the OS version, you can simply check whether "Documents" exists and if not then try "My Documents" or vice versa. This isn't perfect however, because s/o could have a "Documents" folder on his XP machine.
Btw: my system is German, so the folder is called "Dokumente". You might need to take that into account.
The path to that folder is stored in
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
under Personal
. You need registry access, though.
Source: Microsoft
There's no inbuilt enviornment variable, but in PowerShell you can find the location with:
[Environment]::GetFolderPath("mydocuments")
You can also obviously create an environment variable with:
$env:DOCUMENTS = [Environment]::GetFolderPath("mydocuments")

- 110,530
- 99
- 389
- 494

- 505
- 4
- 8
-
1I was struggling to get the syntax just right converting from .net to powershell. No need to use the enum for the param if a string will do. – Craig.C Feb 15 '18 at 11:11
-
1@Craig.C Agreed, but for those who don't like magic strings, like me, there is an enum: `[Environment]::GetFolderPath([Environment+SpecialFolder]::MyDocuments` – Alan McBee Jul 31 '18 at 22:47
-
@AlanMcBee-MSFT Great. Do you have to use the + a lot in powershell then? I've always tried a . instead as you would in C# and got nowhere. – Craig.C Aug 10 '18 at 12:01
-
@Craig.C The + there isn't a feature of PowerShell, exactly. The expression `[Environment+SpecialFolder]` denotes the nested type `SpecialFolder`, which is an enum type that is nested in the `Environment` static class. Using the + to precede the name of nested class is a common representation for a nested class. See https://learn.microsoft.com/en-us/dotnet/api/system.type.gettype?view=netframework-4.7.2#System_Type_GetType_System_String_ (in the Remarks section) – Alan McBee Aug 10 '18 at 18:55
(Just to reiterate the previous answers) There is no environment variable provided out-of-the-box (WinXP) for the "My Documents" directory.
However, you can set a variable, with the following command:
Tested on Windows 7 / 8.1:
for /f "tokens=3* delims= " %a ^
in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') ^
do (set mydocuments=%a %b)
or (one liner)
for /f "tokens=3* delims= " %a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set mydocuments=%a %b)
Which would then give you a %mydocuments%
variable:
C:\>echo mydocuments="%mydocuments%"
mydocuments="C:\pathto\My Documents"
(Does anyone use XP/Vista? If yes, can test this and let us know if it works?)

- 24,216
- 9
- 104
- 119
-
1Hi! Thanks for your answer. On my Windows 7 computer there are spaces in the path. For example "\\drive\users\username\My Documents\". The %UserDocuments% variable returns this: "\\drive\users\username\My". Is that possible to solve? – Stefan Edberg May 27 '15 at 11:29
-
@StefanEdberg, I'm able to reproduce your issue at work. Bascially the "Documents" is being parsed as a second token. The quick fix, would be to use the `%b` variable, like this: `FOR /F "tokens=3* delims= " %a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set mydocuments=%a %b)` <--. – Nick Grealy May 28 '15 at 03:09
-
1This will fail if there is more than one space in path. Correct would be `tokens=2*` and then `set mydocuments=%b`. – Tithen-Firion Feb 18 '18 at 21:15
-
It doesn't work for me on Windows 8.1. I'm not even sure of what you're trying to do. – KeyC0de Oct 18 '19 at 09:24
If you type:
set
In a command prompt you will get a list of all environment variables defined on your system.
Looking at the ones defined on mine (Windows 7 Home Premium) none of them appear to point towards My Documents.
FYI:
The SHGetSpecialFolderPath function can be used to get the path to the My Documents directory. Alternatively the Environment.GetFolderPath method can be used under .Net

- 84,773
- 49
- 224
- 367
C:\Documents and Settings\mrabinovitch>set | grep -i document
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\myuser\Application Data
HOMEPATH=\Documents and Settings\myuser
USERPROFILE=C:\Documents and Settings\myuser
as you can see there is no such a vairable.

- 1,162
- 13
- 30
In addition to answers based on registry, .NET and PowerShell, you could also use WshSpecialFolders
from WSH. Here's a self-contained command/batch script demonstrating how:
@echo off
call :script > "%temp%\%~n0.js" && cscript //nologo "%temp%\%~n0.js" %*
goto :EOF
:script
echo var specialFolders = WScript.CreateObject('WScript.Shell').SpecialFolders;
echo if (WScript.Arguments.length === 0) {
echo for (var e = new Enumerator(specialFolders); !e.atEnd(); e.moveNext()) {
echo WScript.Echo(e.item());
echo }
echo } else {
echo for (var e = new Enumerator(WScript.Arguments); !e.atEnd(); e.moveNext()) {
echo WScript.Echo(specialFolders(e.item()));
echo }
echo }
goto :EOF
It emits a WSH script in JScript and uses it to get one or more paths for special folder tokens supplied as arguments. Assuming you save the above script as a file called specialf.cmd
, the usage for getting path to current user's documents directory would be:
specialf MyDocuments
Here's another usage testing all special folder tokens:
specialf ^
AllUsersDesktop ^
AllUsersStartMenu ^
AllUsersPrograms ^
AllUsersStartup ^
Desktop ^
Favorites ^
Fonts ^
MyDocuments ^
NetHood ^
PrintHood ^
Programs ^
Recent ^
SendTo ^
StartMenu ^
Startup ^
Templates
You could use this to capture into an environment variable like this:
for /f "delims=/" %p in ('specialf MyDocuments') do @set MYDOCS=%p
Some confusion may be due to the availability of CSIDL/KNOWNFOLDERID values vs command shell environment variables.

- 22,600
- 28
- 79
- 90

- 106
- 5
-
`CSIDL`s can be used in PowerShell with `SheGetSpecialFolderPath`. See [Justin's answer](http://stackoverflow.com/a/3492999/712526) for details. – jpaugh Aug 17 '16 at 13:32
For a batch file in Windows 7 (at least), Nick G's solution needs a slight tweak to set the user-defined variable UserDocuments :
FOR /F "tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set UserDocuments=%%a)
Note the only differences are,
- Use only one space character for delims
- %%a instead of %a
To avoid seeing the line, but to see the results, use :
@FOR /F "tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @(Set UserDocuments=%%a)
@Echo ~~~~~~~~ UserDocuments=%UserDocuments%
Thanks Nick G. Your answer taught me a lot. I hope this helps someone else.

- 456
- 4
- 13
-
Hi! Thanks for your answer. On my Windows 7 computer there are spaces in the path. For example "\\drive\users\username\My Documents\". The %UserDocuments% variable returns this: "\\drive\users\username\My". Is that possible to solve? – Stefan Edberg May 27 '15 at 08:19
Improved @NickGrealy answer:
reg query
outputs
empty_line reg_key_path name type value
- there can be an arbitrary amount of 'space chars' between words in a registry value, and the
%a %b
string is not correct in this case
So, using the skip=2
option to skip first lines and the tokens=2*
option to pass a registry value to the %b
var:
for /f "skip=2 tokens=2*" %A in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "UserDocs=%B"
or for script files:
for /f "skip=2 tokens=2*" %%A in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do set "UserDocs=%%B"
But taking into account the registry value [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\!Do not use this registry key]
Based on @AtifAziz answer:
for /f "tokens=*" %A in ('echo WScript.Echo^(^(new ActiveXObject^("WScript.Shell"^)^).SpecialFolders^("MyDocuments"^)^)^>%TEMP%\getdoc.js ^& cscript /nologo %TEMP%\getdoc.js ^& del /q %TEMP%\getdoc.js') do @set "UserDocs=%A"

- 3,118
- 1
- 30
- 35
There does not exist by design a documents environment variable in windows. You have to create a customized one. Do this by going here. Define an environment variable called MYDOCUMENTS to reference whichever location you need referenced. Thereafter, it shall be an environment variable that you reference by %MYDOCUMENTS%.

- 6,115
- 16
- 50
- 57

- 11
- 1
-
1Generally speaking, the point of reading an environmental variable is to get information on machines other than your own where you already know the value. In order to set such a custom variable, you would still need to get it programmatically first! – BuvinJ Jan 26 '16 at 17:43
Tested and worrking in win XP, vista, 8, 8.1 and 10!!
@echo off
for /f "skip=2 tokens=2*" %%c in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "docs=%%d" && echo WIN XP - 10
xcopy "C:\test.txt" "%docs%" /f /y
echo %docs%
pause
EXIT

- 11
- 1
Windows Batch File (.bat) or Windows Command Script (.cmd)
@echo off
ver | find "XP" > nul
if %ERRORLEVEL% == 0 set Docs=%UserProfile%\My Documents & echo WIN XP
if %ERRORLEVEL% == 1 set Docs=%UserProfile%\Documents & echo WIN vista - 10
xcopy "C:\test.txt" "%Docs%" /f /y
pause
EXIT

- 2,777
- 3
- 24
- 32
update Windows Batch File (.bat) or Windows Command Script (.cmd)
@echo off
ver | find "XP" > nul
if %ERRORLEVEL% == 0 SET DOCS=%USERPROFILE%\My Documents & echo WIN XP
if %ERRORLEVEL% == 1 FOR /f "tokens=3" %%x IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set docs=%%x) & echo WIN vista - 10
xcopy "C:\test.txt" "%docs%" /f /y
echo %docs%
pause
EXIT
-
this doesn't make sense in any language version but English, e.g. in German `%USERPROFILE%\My Documents` is `%USERPROFILE%\Dokumente`, in Polish `%USERPROFILE%\Moje Dokumenty`, however Win XP running is rather rare phenomenon, so I'd remove it at all – Marcin Tarsier Mar 27 '18 at 15:36
Here's the full list of user variables for all users running Windows.
The reg query portion of the code will find that value and set it so it can be used immediately and allows the other codes to set it permanently for all users. The ad registry portion of the code will enable it for all users. The setx makes it so you don't have to log off and log back in to be able to use the codes.
The current user variables only applies to the person who installed this code.
@ECHO OFF
ECHO REG ALL USER variables
ECHO all user desktop
for /f "skip=2 tokens=3*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Desktop"') do @set "ALLDT=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLDT" /t "REG_SZ" /d "%ALLDT%" /f
setx ALLDT "%ALLDT%"
Echo all user's documents
for /f "skip=2 tokens=3*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Documents"') do @set "ALLDOC=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLDOC" /t "REG_SZ" /d "%ALLDOC%" /f
setx ALLDOC "%ALLDOC%"
echo all user start menu
for /f "skip=2 tokens=3*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Programs"') do @set "ALLSM=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLSM" /t "REG_SZ" /d "%ALLSM%" /f
setx ALLSM "%ALLSM%"
Echo all users startup
for /f "skip=2 tokens=3*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Startup"') do @set "ALLSMSTU=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLSMSTU" /t "REG_SZ" /d "%ALLSMSTU%" /f
setx ALLSMSTU "%ALLSMSTU%"
Echo all users music
for /f "skip=2 tokens=2*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "CommonMusic"') do @set "ALLMU=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLMU" /t "REG_SZ" /d "%ALLMU%" /f
setx ALLMU "%ALLMU%"
echo all users pictures
for /f "skip=2 tokens=2*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "CommonPictures"') do @set "ALLPIC=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLPIC" /t "REG_SZ" /d "%ALLPIC%" /f
setx ALLPIC "%ALLPIC%"
Echo all users videos
for /f "skip=2 tokens=2*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "CommonVideo"') do @set "ALLVID=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLVID" /t "REG_SZ" /d "%ALLVID%" /f
setx ALLVID "%ALLVID%"
Echo set cerrent user variables
Echo current users desktop
for /f "skip=2 tokens=2*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"') do @set "myDesktop=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myDesktop" /t "REG_SZ" /d "%myDesktop%" /f
setx myDesktop "%myDesktop%"
Echo current users documents
for /f "skip=2 tokens=2*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "mydoc=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "mydoc" /t "REG_SZ" /d "%mydoc%" /f
setx mydoc "%mydoc%"
Echo current user start menu
for /f "skip=2 tokens=3*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Start Menu"') do @set "myStart=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myStart" /t "REG_SZ" /d "%myStart%" /f
setx myStart "%myStart%"
Echo current user startup
for /f "skip=2 tokens=2*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Startup"') do @set "myStartup=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myStartup" /t "REG_SZ" /d "%myStartup%" /f
setx myStartup "%myStartup%"
Echo current users music
for /f "skip=2 tokens=3*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Music"') do @set "myMU=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myMU" /t "REG_SZ" /d "%myMU%" /f
setx myMU "%myMU%"
Echo current users pictures
for /f "skip=2 tokens=3*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Pictures"') do @set "myPIC=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myPIC" /t "REG_SZ" /d "%myPIC%" /f
setx myPIC "%myPIC%"
Echo current users video
for /f "skip=2 tokens=3*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Video"') do @set "myVID=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myVID" /t "REG_SZ" /d "%myVID%" /f
setx myVID "%myVID%"
exit
Actually, the %USERPROFILE%\My Documents
should work in Windows 7. It's what I use.

- 22,960
- 16
- 44
- 73

- 19
-
Three years after the question was asked, you leave a one-line answer and don't ever bother to read the current best answer (which would have shown you your mistake). – Ben Voigt Feb 27 '14 at 17:02
-
2The mistake is not taking locale into account? Cause at least on english systems, %userprofile%\My Documents is a symbolic link to %userprofile%\Documents in Windows Vista and above. This is for backwards compatibility with Windows XP. – jpkotta Oct 24 '14 at 20:55