windowspowershellbatch-fileenvironment-variablesspecial-folders

%USERPROFILE%/Desktop no longer valid after relocating Desktop folder to OneDrive


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?


Solution

  • 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