1

I've moved my Windows 10's /Desktop folder to a different location.

Desktop properties

As a result, my batch and Powershell scripts that were pointing to %USERPROFILE%/Desktop no longer worked. Is there another way to get the location of my desktop without hardcoding the new path in?

Cardin
  • 5,148
  • 5
  • 36
  • 37
  • 2
    As a workaround for scripts that assume the default path (they shouldn't), you can create a bind mount (junction) or directory symlink at the default path that targets the configured path. If "D:\OneDrive\Desktop" is local (i.e. "D:" is a local drive), use a bind mount. If it's remote, use a directory symlink and ensure that local-to-remote (L2R) symlink evaluation is enabled for the system (it should be enabled by default). – Eryk Sun Oct 08 '20 at 06:36
  • 3
    `%USERPROFILE%/Desktop` was never correct although working usually. Correct would be `%USERPROFILE%\Desktop` for default configurations as the directory separator on Windows is ``\`` and not `/` as on Linux/Mac. See the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file) for details. See [this answer](https://stackoverflow.com/a/56454730/3074564) and try the batch code posted there from within a command prompt window to see if this batch file detects the correct directory for user's desktop on your Windows PC. – Mofi Oct 08 '20 at 07:22
  • @Mofi Your referenced batch script works too! – Cardin Oct 08 '20 at 07:49
  • @ErykSun That's pretty ingenious! – Cardin Oct 08 '20 at 07:49

1 Answers1

6

In PowerShell you can use this

[Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop)

To use it from a batch file you can call powershell to get the path

powershell -C "[Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop)"

and then save the result to a variable using for /f

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    For batch scripts, you'd want to parse out the result into an environment variable using a `for /f` loop. – Eryk Sun Oct 08 '20 at 06:27