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

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


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, objFSOset objFSO=CreateObject("Scripting.FileSystemObject")Set WSHshell = CreateObject("WScript.Shell")desktop = WSHShell.SpecialFolders("Desktop")pathstring = objFSO.GetAbsolutePathName(desktop)WScript.Echo pathstring


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

Here is the batch file:

@ECHO OFFFOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%iECHO %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).

HTH,

Iulian Şerbănoiu


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 DesktopMy documents turning to Meus Documentos and Local Settings to Configuracoes locais Personaly I thinks this is a bad thing when projecting a OS.