Get path of the Desktop folder

I have a batch script that need the full path of the Desktop folder.

Right now I am using %USERPROFILE%/Desktop, it works fine until I learned that the path can be changed (See Can you change the location of the Desktop folder in Windows?).

I tried to read the regedit and I end up with something like this (I have cygwin installed):

REG QUERY "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\Current
Version\Explorer\Shell Folders" /v Desktop |grep Desktop| awk '{$1=$2=""; print $0}'

This is working fine but I don't like it for the sake of robust and simplicity. Not to metion that it will be even more messy if I don't have awk avaliable.

As I noticed, you can type shell:Desktop in File Explorer and it will open the Desktop folder for you. However I have no clue how I can use that in a batch file.

Is there a hidden environment variable or simple command I can use to retrive this value?

1 Answer

There are no environment variables for the majority of special folders. The official method is to call either SHGetKnownFolderPath() from Win32 API or System.Environment.GetFolderPath() from .NET – the latter is easily accessible through PowerShell:

$dir = [Environment]::GetFolderPath("Desktop")

Batch scripts (Cmd.exe) do not have access to such features; you would have to call a program that does (such as PowerShell or third-party tools like nircmd).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like