How do I redirect a folder programmatically? Specially Videos and Downloads? via Powershell?

Joan Montas 1 Reputation point
2021-08-08T06:20:37.643+00:00

How do I change the file location in windows 10? Here is how to do it manually:

121356-manual.png

Specifically those Shortcut under This PC (Specifically Downloads and Videos, As I know how to edit quick access via registry, Note that changing the registry did change all the PC shortcut except Videos and Downloads...please look at Highlight 1...Video nor Download are shown):
121339-thispcshortcut.png

I have modified the location like so: Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' -Name Favorites -Value "%USERPROFILE%\OneDrive - StackOverFlow\Favorites

But Downloads and Videos does not seems be affect i.e the shortcuts under This Pc (Not under Quick Access)

Please help, Thanks!

PS I know it can be done via GPO but I am looking for a more control using powershell and or cmd

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Spigolo 626 Reputation points Volunteer Moderator
    2021-08-08T13:43:24.437+00:00

    Hi @Joan Montas

    From A Command Prompt or Powershell (admin) type:

    reg export "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" %userprofile%\desktop\KeyBackup.reg
    (this is to save previous settings)

    reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v {374DE290-123F-4565-9164-39C4925E467B} /d "X:\MyDownloads"

    reg add
    "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v {7D83EE9B-2244-4E70-B1F5-5393042AF1E4} /d "X:\MyDownloads"

    reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Video" /d "X:\MyVideos"

    Close everything and restart.

    1 person found this answer helpful.
    0 comments No comments

  2. Castorix31 90,681 Reputation points
    2021-08-08T08:43:31.187+00:00

    When you do it manually, Explorer calls :

    IKnownFolderManager::Redirect

    So you can do the same thing, but it is easier in C++, C# or VB than in PowerShell...
    (I just tested in C++ with Download folder and it worked correctly)


  3. Castorix31 90,681 Reputation points
    2021-08-08T13:18:45.173+00:00

    [I cannot post the code in comments]

    The test in C++ where I changed "C:\Users\Christian\Downloads" to "C:\Users\Christian\Source"

    IKnownFolderManager* pKnownFolderManager = NULL;
    HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_ALL, IID_IKnownFolderManager, (void**)&pKnownFolderManager);
    if (SUCCEEDED(hr))
    {
     PWSTR pwszDownloadPath = NULL;
     hr = SHGetKnownFolderPath(FOLDERID_Downloads, 0, NULL, &pwszDownloadPath); // "C:\\Users\\Christian\\Downloads"
     WCHAR wszNewPath[MAX_PATH] = L"C:\\Users\\Christian\\Source";
     IKnownFolder* pKnownFolder;
     hr = pKnownFolderManager->FindFolderFromPath(pwszDownloadPath, FFFP_EXACTMATCH, &pKnownFolder);
     if (SUCCEEDED(hr))
     {
     KNOWNFOLDERID kfid = { 0 };
     hr = pKnownFolder->GetId(&kfid);
     DWORD dwFlags = KF_REDIRECT_WITH_UI | KF_REDIRECT_PIN; // random flags, to be tested with other flags...
     PWSTR pwszError;
     hr = pKnownFolderManager->Redirect(kfid, hWnd, dwFlags, wszNewPath, 0, NULL, &pwszError);
     // Notifications
     PIDLIST_ABSOLUTE pidlUsersFiles;
     if (SUCCEEDED(SHGetKnownFolderIDList(FOLDERID_UsersFiles, 0, NULL, &pidlUsersFiles)))
     {
     SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_IDLIST, pidlUsersFiles, NULL);
     ILFree(pidlUsersFiles);
     }
     PIDLIST_ABSOLUTE pidlDownloadFolder;
     if (SUCCEEDED(SHGetKnownFolderIDList(kfid, 0, NULL, &pidlDownloadFolder)))
     {
     SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_IDLIST, pidlDownloadFolder, NULL);
     ILFree(pidlDownloadFolder);
     }
     }
    }
    

    includes/lib :

    #include <KnownFolders.h>
    #include <Shobjidl.h>
    #include <shlwapi.h>
    #pragma comment(lib, "Shlwapi")
    #include <strsafe.h>
    #include <Shlobj.h>
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.