What's the environment variable for the path to the desktop?

227,065

Solution 1

I found that the best solution is to use a vbscript together with the batch file.

Here is the batch file:

@ECHO OFF
FOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%

Here is findDesktop.vbs file:

set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)

There may be other solutions but I personally find this one less hackish.

I tested this on an English PC and also a French PC - it seems to work (Windows XP).

Solution 2

To be safe, you should use the proper APIs in Powershell (or VBScript)
Using PowerShell:

[Environment]::GetFolderPath("Desktop")

Copy something using Powershell:

Copy-Item $home\*.txt ([Environment]::GetFolderPath("Desktop"))

Here is a VBScript-example to get the desktop path:

dim WSHShell, desktop, pathstring, objFSO
set objFSO=CreateObject("Scripting.FileSystemObject")
Set WSHshell = CreateObject("WScript.Shell")
desktop = WSHShell.SpecialFolders("Desktop")
pathstring = objFSO.GetAbsolutePathName(desktop)
WScript.Echo pathstring

Solution 3

EDIT: Use the accepted answer, this will not work if the default location isn't being used, for example: The user moved the desktop to another drive like D:\Desktop


At least on Windows XP, Vista and 7 you can use the "%UserProfile%\Desktop" safely.

Windows XP en-US it will expand to "C:\Documents and Settings\YourName\Desktop"
Windows XP pt-BR it will expand to "C:\Documents and Settings\YourName\Desktop"
Windows 7 en-US it will expand to "C:\Users\YourName\Desktop"
Windows 7 pt-BR it will expand to "C:\Usuarios\YourName\Desktop"

On XP you can't use this to others folders exept for Desktop My documents turning to Meus Documentos and Local Settings to Configuracoes locais Personaly I thinks this is a bad thing when projecting a OS.

Solution 4

KB's answer to use [Environment]::GetFolderPath("Desktop") is obviously the official Windows API for doing this.

However, if you're working interactively at the prompt, or just want something that works on your machine, the tilda (~) character refers to the current user's home folder. So ~/desktop is the user's desktop folder.

Solution 5

Not only would that not work for an International version of Windows, it would fail if the user had edited the Registry to make their Desktop folder reside somewhere else. You can query the Registry for the file location using the REG command:

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop

To get this into a variable use something like this:

FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
Share:
227,065
Scott Langham
Author by

Scott Langham

Updated on October 09, 2021

Comments

  • Scott Langham
    Scott Langham over 2 years

    I'm writing a Windows batch file and want to copy something to the desktop. I think I can use this:

    %UserProfile%\Desktop\

    However, I'm thinking, that's probably only going to work on an English OS. Is there a way I can do this in a batch file that will work on any internationalized version?

    UPDATE

    I tried the following batch file:

    REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
    FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
    FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
    ECHO %DESKTOPDIR%
    

    And got this output:

    S:\>REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
    
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
        Desktop    REG_EXPAND_SZ    %USERPROFILE%\Desktop
    
    
    S:\>FOR /F "usebackq tokens=3 skip=4" %i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folder
    s" /v Desktop`) DO SET DESKTOPDIR=%i
    
    S:\>FOR /F "usebackq delims=" %i in (`ECHO ECHO is on.`) DO SET DESKTOPDIR=%i
    
    S:\>SET DESKTOPDIR=ECHO is on.
    
    S:\>ECHO ECHO is on.
    ECHO is on.
    
  • Pekka
    Pekka over 14 years
    For me, on Windows 7, that returns Desktop REG_EXPAND_SZ %USERPROFILE%\Desktop which is correct but probably won't help the OP in his batch file, as he probably won't be able to parse the environment variable. Or would he just have to use it and it would parse itself?
  • David Webb
    David Webb over 14 years
    Have added some code to parse the output of REG.EXE to the answer.
  • Scott Langham
    Scott Langham over 14 years
    Thanks for the tip Dave. I'm not quite there yet though - think I'm missing something. I've updated the question to show where I've got to. If you've got any more advice, it would be appreciated.
  • Rich
    Rich over 14 years
    Keep in mind that querying this from the registry is discouraged. Kb's VBScript solution is therefore the one that should be used to ensure that this works.
  • Rich
    Rich over 14 years
    +1 for using the proper APIs for this. The registry shouldn't be used. (Anyone not getting this: Read Raymond Chen's blog. Yes, completely.) :-)
  • Kb.
    Kb. almost 14 years
    This is right. +1 And I beieve the desktop will always be "desktop"
  • oleschri
    oleschri about 11 years
    You cannot assume ~/desktop works, because it can be redirected for roaming profiles. I am in that situation ;)
  • Qben
    Qben over 10 years
    It will not work for a Swedish version since it's not called Desktop in Swedish but Skrivbord. Hence, %UserProfile%\Skrivbord is the proper use for a Swedish version, and %UserProfile%\Desktop is invalid.
  • Jochem Kuijpers
    Jochem Kuijpers over 9 years
    I moved my desktop to E:\Desktop. This will not work then. I'm running Windows 8.1, but I'm fairly sure you could do this on Windows 7 as well. Please don't do this as this is the most common cause of a lost icon.
  • Steve Waring
    Steve Waring almost 9 years
    Tis will not work if you have moved your desktop, as I have. If you have a solid state C: drive it is common practice to move your desktop onto another drive so the SSD does not get hammered with lots of writes.
  • Daniel Cheung
    Daniel Cheung almost 9 years
    it wouldn't work if desktop is moved to another location
  • Matthew McDermott
    Matthew McDermott almost 8 years
    I liked this on for my profile. Set-Location $([Environment]::GetFolderPath("Desktop")) is just what I was looking for.
  • Pacerier
    Pacerier over 7 years
  • Liggliluff
    Liggliluff over 7 years
    @Qben, the English terms will always use regardless of language. %UserProfile%\Desktop will go to %UserProfile%\Skrivbord.
  • Liggliluff
    Liggliluff over 7 years
    I have located my Desktop folder at E:\Users\YourName\Desktop, but if you use %UserProfile%\Desktop you will reach C:\Users\YourName\Desktop, of which is empty.
  • zxq9
    zxq9 over 4 years
    Localized names are all aliases that point toDesktop (we use this safely here in Japan, Thailand, Israel and the US). The difference is whether %UserProfile% points to the user's actual profile base dir. I'm not a Windows dev, but from cross-platform dev what I've found is the profile dir is for settings, but the HOME dir is for the user's own files, so Desktop/Downloads/Pictures/etc. IIRC %HOMEDRIVE%%HOMEPATH%\Desktop is the safest way.
  • Cestarian
    Cestarian over 3 years
    I'm surprised this isn't marked as the chosen answer, since this is the best answer so far imo. However @zxq9 you probably should make your own answer with the solution you provided.