Windows CMD does not see Desktop when I move it around

Page 2 of 4 FirstFirst 1234 LastLast

  1. Posts : 17,661
    Windows 10 Pro
       #11

    Try3 said:
    Cmd cannot resolve %UserProfile%\Desktop properly to find a relocated Desktop. It has never been able to do so on any of my Windows 7 or Windows 10 computers.
    Since early days of Vista beta builds, I have used Sysprep to relocate Users folder, all user profiles and their subfolders to another drive. It is 100% safe, easy procedure. I have no issues to use %userprofile% environment variable.

    Windows CMD does not see Desktop when I move it around-image.png

    Move Users Folder Location in Windows 10

    Kari
      My Computer


  2. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #12

    Find the user Desktop folder path - PS, vbs, batch methods


    Updated 19th March 2021 - I have improved the treatment of spaces and ampersands in the Desktop folder path.

    Windows' command-line processor cannot resolve %UserProfile%\Desktop properly to find a relocated Desktop.
    - It has never been able to do so on any of my Windows 7 or Windows 10 computers.
    - I noted what Kari said above about the case of having moved the entire C:\Users folder not being a problem but that is not what this thread has been about.

    Solutions to retrieve the user Desktop folder path even if it has been relocated:
    - These scripts cope with Desktop folder paths that contain spaces and ampersands [such as those that UserName Tom & Jerry might have].
    - These scripts have been tested with Desktop folder paths that contain explicit spaces and ampersands [such as C:\Users\Tom & Jerry\Desktop]. {This test was added today. My previous batch file only coped with implicit spaces and ampersands.}
    - These scripts have been tested with Desktop folder paths that contain implicit spaces and ampersands [such as those in the variable %UserProfile% for UserName Tom & Jerry].
    - These scripts were written in the expectation that they would be used as subroutines to set variables for use elsewhere. Each script has nevertheless been altered to allow it to remain open & display a result.

    PS solution [simplified 6th Dec 2021]
    Code:
    $UserDesktop = [environment]::GetFolderPath('Desktop')
    ## The following lines are just there to make this a demonstrator version
    $UserDesktop
    Pause

    VBS solution
    Code:
    UserDesktop = WScript.CreateObject("WScript.Shell").SpecialFolders("Desktop")
    ' The following MsgBox line is just there to make this a demonstrator version
    MsgBox UserDesktop
    - This method is based on sample code provided in the 2001 version of VBS Help that was, I think, included in WinXP.

    Batch file solution
    Code:
    :: Purpose - Get the user's Desktop path even if it has been relocated and even if the path contains spaces and ampersands
    
    :: Extract the Desktop folder path from the Registry
    set UseExpresssion=Reg Query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
    For /F "tokens=* " %%X in (' %UseExpresssion% ') Do Set "DesktopFolder=%%X"
    
    :: Remove unwanted preamble from the extracted value
    Set "DesktopFolder=%DesktopFolder:*REG_EXPAND_SZ    =%"
    
    :: Resolve a result that contains a variable such as %UserProfile%
    For /F "delims=" %%X in ('Echo "%DesktopFolder%"') Do Set "DesktopFolder=%%X"
    
    :: Strip quotation marks from start and end of DesktopFolder
    Set "DesktopFolder=%DesktopFolder:~1,-1%"
    
    :: Always protect use of DesktopFolder with quotation marks unless and until it is proven to contain no spaces or special characters such as ampersand
    echo DesktopFolder - "%DesktopFolder%"
    
    ::The following Pause line is just there to make this a demonstrator version
    Pause to review results during testing

    Denis
    Last edited by Try3; 06 Dec 2021 at 06:43.
      My Computer


  3. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #13

    Kari said:
    I have used Sysprep to relocate Users folder, all user profiles and their subfolders to another drive. ... I have no issues to use %userprofile% environment variable.
    Kari,

    I believe that all three solutions will also work for the case you have described including where C:\Users has been moved and then the individual user folder Desktop has been further relocated.

    Denis
      My Computer


  4. Posts : 989
    Microsoft Windows 10 Home
       #14

    Matthew Wai said:
    @Try3
    After moving my desktop, the following PowerShell command does not work. How would you fix it?

    (systeminfo) >> "$env:homeDrive$env:homePath\Desktop\My PC info.txt"
    None of the environmetal variables are aware of redirected folders. You can query the registry deriectly:
    Code:
    $Path = (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders').Desktop
    Or use a com object to work with the Shell:
    Code:
    $Path = (New-Object -ComObject Wscript.Shell).SpecialFolders(4)
      My Computer


  5. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #15

    lqlaow said:
    on windows 10 1809, I prefer to have my Desktop folder on D:
    lqlaow,

    I have amended the batch file solution in post #12 to make it more generally applicable. It can now cope if the Desktop folder path contains spaces or ampersands.

    Denis
      My Computer


  6. Posts : 7,607
    Windows 10 Home 20H2
       #16

    Am I wrong in thinking that a user who uses a desktop path containing spaces or ampersands is looking for trouble?
      My Computer


  7. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #17

    Yes
      My Computer


  8. Posts : 4
    Windows 10 Home Edition
       #18

    Question about the code


    Try3 said:
    Windows' command-line processor cannot resolve %UserProfile%\Desktop properly to find a relocated Desktop.
    - It has never been able to do so on any of my Windows 7 or Windows 10 computers.
    - I noted what Kari said above about the case of having moved the entire C:\Users folder not being a problem but that is not what this thread has been about.

    Solutions to retrieve the user Desktop folder path even if it has been relocated -

    PS solution
    Code:
    $path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
    $UserDesktop = (Get-ItemProperty -Path $path -Name "Desktop").Desktop
    echo $UserDesktop
    ## Pause during testing only
    Pause
    - I found this method in How do I get the value of a registry key and ONLY the value using powershell - StackOverflow
    - Guidance is also in Working with Registry Entries - PowerShell - MSDocs

    VBS solution
    Code:
    UserDesktop = WScript.CreateObject("WScript.Shell").SpecialFolders("Desktop")
    ' The following MsgBox line is just there to make this a demonstrator version
    MsgBox UserDesktop
    - This method is based on sample code provided in the 2001 version of VBS Help that was, I think, included in WinXP.

    Batch file solution - updated 17th August 2020 - now handles spaces and ampersands in Desktop paths
    Code:
    :: Purpose - Get the user's Desktop path even if it has been relocated and even if the path contains spaces or ampersands
    
    :: Extract the Desktop folder path from the Registry
    set UseExpresssion=Reg Query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
    For /F "usebackq tokens=3 " %%X in (` %UseExpresssion% `) Do Set DesktopFolder=%%X
    
    :: Resolve a result that contains a variable such as %UserProfile%
    For /F "usebackq delims=" %%X in (`Echo "%DesktopFolder%"`) Do Set "DesktopFolder=%%X"
    
    :: Strip quotation marks from start and end of DesktopFolder
    Set "DesktopFolder=%DesktopFolder:~1,-1%"
    
    :: Always protect use of DesktopFolder with quotation marks unless and until it is proven to contain no spaces or special characters such as ampersand
    echo DesktopFolder - "%DesktopFolder%"
    
    Pause to review results during testing

    Denis
    I had tried to use the batch file script you had written and while it did work for finding my desktop location on another drive, it could not find "My Music" folder on another drive or any of the other per user account folders.

    I did change the folder to search in the code but instead of the redirected path (being "E:\Music"), it returned "REG_EXPAND_SZ".

    Below is the modified code:
    Code:
    :: Purpose - Get the user's Music path even if it has been relocated and even if the path contains spaces or ampersands
    
    :: Extract the Music folder path from the Registry
    set UseExpresssion=Reg Query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Music"
    For /F "usebackq tokens=3 " %%X in (` %UseExpresssion% `) Do Set MusicFolder=%%X
    
    :: Resolve a result that contains a variable such as %UserProfile%
    For /F "usebackq delims=" %%X in (`Echo "%MusicFolder%"`) Do Set "MusicFolder=%%X"
    
    :: Strip quotation marks from start and end of MusicFolder
    Set "MusicFolder=%MusicFolder:~1,-1%"
    
    :: Always protect use of MusicFolder with quotation marks unless and until it is proven to contain no spaces or special characters such as ampersand
    echo MusicFolder - "%MusicFolder%"
    
    ::Pause to review results during testing
    pause
      My Computer


  9. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #19

    I never claimed that other user folders could just be substituted into that code without any adjustment being made.

    Examine the contents of the Command window when you run it and you will probably spot the problem.

    If not, just run the Reg command on its own in a Command window
    Code:
    Reg Query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Music"
    or run RegEdit & go to that Registry key to have a look
    and you will probably then be able to see what's happening and what to change.

    and/or, in a Command window, enter
    Code:
    For /?
    and study its guidance.

    Denis
    Last edited by Try3; 19 Sep 2020 at 11:23.
      My Computer


  10. Posts : 989
    Microsoft Windows 10 Home
       #20

    Many of the user-specific system folders don't have an entry under:
    HKCU\...\User Shell Folders unless they've been relocated. Better to "ask the shell". This PowerShell demonstrates.

    Code:
    $USF = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
    $FD  = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions'
    
    $USFItems = ($USF | Get-Item).Property
    
    $Shell = New-Object -ComObject shell.application
    
    $FD | Get-ChildItem | Get-ItemProperty | Where Category -eq 4 |
        select @{ N = 'In USF?'   ; E = { ($_.Name -in $USFItems) -or ($_.PSChldName -in $USFItems)  }} ,
               @{ N = 'GUID'      ; E = { $_.PSChildName }} ,
               Name ,
               @{ N = 'Command'   ; E = { "shell:$($_.Name)" }} ,
               @{ N = 'Path'      ; E = { $Shell.Namespace( "shell:$($_.Name)" ).Self.Path  }} |
    Out-GridView
    Windows CMD does not see Desktop when I move it around-screenshot-846-.png
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:56.
Find Us




Windows 10 Forums