Search code examples
windowsjunction

Move windows users folder to external drive while keeping junctions


I've found and followed these methods* to move my user folder to an external drive, and it works wonderfully, however a side effect of this is that robocopy source dest /xj copies all files BUT junctions, meaning that the user folder it creates on the external drive has no directory junctions anymore, like it does in default Windows 7. I've tried everything I can to move junctions from backups, create new ones with the same properties, copy them, etc, and nothing I've tried has worked to get them back.

I need a way to either fix my junction links in my user folder, A way to copy / move some from my backups, Or a way to follow the steps in the link, but keep all junctions intact.

You may ask why I'm so concerned with junctions when the user profile appears to be working fine? The reason is that these junctions are there to keep backward compatibility with previous operating systems, and should be there. I want to copy the EXACT users folder over, not most of it. There is honestly no reason why this should be so hard. :/

Does anyone have any suggestions? Thanks!

Note: I've done extensive Googling for hours, and cannot come up with any solution to the problem. This is my last resort. I don't mind the answers that come, even if it's a reinstall. Just as long as there's a way to do it.

* http://lifehacker.com/5467758/move-the-users-directory-in-windows-7


Solution

  • Since I couldn't find any real answers anywhere, I took the time to write and reverse engineer the entire process.

    I hope this is of use to anyone else looking to do the same. Please enjoy!

    Note: These scripts are designed to only work with one user on the system, other than the default ones. It will not generate anything for additional users you may have created.

    WARNING: These scripts come with no warranty, and while I have done my best to make them perfect, I will in no way be liable for any damages you may receive by using them. Use at your own risk!

    #
    # Restore default Windows 7 symlink and junction points
    #
    # To run: 
    # (1) Start CMD as administrator
    # (2) cd to directory where script is
    # (3) Run: powershell -executionpolicy remotesigned -File restorelinks.ps1
    #
    
    
    # tmp folder for our work
    New-Item -ItemType directory -Path .\tmp
    
    $username = [Environment]::UserName
    $userObj = New-Object System.Security.Principal.NTAccount("$username")
    $userSid = $userObj.Translate([System.Security.Principal.SecurityIdentifier]).Value
    
    
    # file, target, perms
    $symlinks = @(,
        ("C:\Users\All Users", "C:\ProgramData", "D:PAI(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICIIO;GA;;;CO)(A;OICI;0x1200a9;;;BU)(A;CI;DCLCRPCR;;;BU)")
    )
    
    # file, target, perms
    $junctions = @(
        ("C:\Users\Default User",                                     "C:\Users\Default",                                                            "D:PAI(D;;CC;;;WD)(A;;0x1200a9;;;WD)(A;;FA;;;SY)(A;;FA;;;BA)"),
        ("C:\Users\Default\Application Data",                         "C:\Users\Default\AppData\Roaming",                                            "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\Cookies",                                  "C:\Users\Default\AppData\Roaming\Microsoft\Windows\Cookies",                  "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\Local Settings",                           "C:\Users\Default\AppData\Local",                                              "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\My Documents",                             "C:\Users\Default\Documents",                                                  "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\NetHood",                                  "C:\Users\Default\AppData\Roaming\Microsoft\Windows\Network Shortcuts",        "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\PrintHood",                                "C:\Users\Default\AppData\Roaming\Microsoft\Windows\Printer Shortcuts",        "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\Recent",                                   "C:\Users\Default\AppData\Roaming\Microsoft\Windows\Recent",                   "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\SendTo",                                   "C:\Users\Default\AppData\Roaming\Microsoft\Windows\SendTo",                   "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\Start Menu",                               "C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu",               "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\Templates",                                "C:\Users\Default\AppData\Roaming\Microsoft\Windows\Templates",                "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\AppData\Local\Application Data",           "C:\Users\Default\AppData\Local",                                              "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\AppData\Local\History",                    "C:\Users\Default\AppData\Local\Microsoft\Windows\History",                    "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\AppData\Local\Temporary Internet Files",   "C:\Users\Default\AppData\Local\Microsoft\Windows\Temporary Internet Files",   "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\Documents\My Music",                       "C:\Users\Default\Music",                                                      "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\Documents\My Pictures",                    "C:\Users\Default\Pictures",                                                   "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Default\Documents\My Videos",                      "C:\Users\Default\Videos",                                                     "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;ID;0x1200a9;;;BU)(A;OICIIOID;GXGR;;;BU)(A;ID;0x1200a9;;;WD)(A;OICIIOID;GXGR;;;WD)"),
        ("C:\Users\Public\Documents\My Music",                        "C:\Users\Public\Music",                                                       "D:PAI(D;;CC;;;WD)(A;;0x1200a9;;;WD)(A;;FA;;;SY)(A;;FA;;;BA)"),
        ("C:\Users\Public\Documents\My Pictures",                     "C:\Users\Public\Pictures",                                                    "D:PAI(D;;CC;;;WD)(A;;0x1200a9;;;WD)(A;;FA;;;SY)(A;;FA;;;BA)"),
        ("C:\Users\Public\Documents\My Videos",                       "C:\Users\Public\Videos",                                                      "D:PAI(D;;CC;;;WD)(A;;0x1200a9;;;WD)(A;;FA;;;SY)(A;;FA;;;BA)"),
        ("C:\Users\$username\Application Data",                       "C:\Users\$username\AppData\Roaming",                                          "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\Cookies",                                "C:\Users\$username\AppData\Roaming\Microsoft\Windows\Cookies",                "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\Local Settings",                         "C:\Users\$username\AppData\Local",                                            "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\My Documents",                           "C:\Users\$username\Documents",                                                "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\NetHood",                                "C:\Users\$username\AppData\Roaming\Microsoft\Windows\Network Shortcuts",      "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\PrintHood",                              "C:\Users\$username\AppData\Roaming\Microsoft\Windows\Printer Shortcuts",      "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\Recent",                                 "C:\Users\$username\AppData\Roaming\Microsoft\Windows\Recent",                 "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\SendTo",                                 "C:\Users\$username\AppData\Roaming\Microsoft\Windows\SendTo",                 "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\Start Menu",                             "C:\Users\$username\AppData\Roaming\Microsoft\Windows\Start Menu",             "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\Templates",                              "C:\Users\$username\AppData\Roaming\Microsoft\Windows\Templates",              "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\AppData\Local\Application Data",         "C:\Users\$username\AppData\Local",                                            "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\AppData\Local\History",                  "C:\Users\$username\AppData\Local\Microsoft\Windows\History",                  "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\AppData\Local\Temporary Internet Files", "C:\Users\$username\AppData\Local\Microsoft\Windows\Temporary Internet Files", "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\Documents\My Music",                     "C:\Users\$username\Music",                                                    "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\Documents\My Pictures",                  "C:\Users\$username\Pictures",                                                 "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)"),
        ("C:\Users\$username\Documents\My Videos",                    "C:\Users\$username\Videos",                                                   "D:AI(D;;CC;;;WD)(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;$userSid)")
    )
    
    # dump acl file
    Add-Content tmp\acl-tmp.dat "Users`r`nD:PAI(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;;0x1200a9;;;BU)(A;OICIIO;GXGR;;;BU)(A;;0x1200a9;;;WD)(A;OICIIO;GXGR;;;WD)"
    foreach ($symlink in $symlinks) {
        Add-Content tmp\acl-tmp.dat "$($symlink[0].Substring(3))`r`n$($symlink[2])"
    }
    foreach ($junction in $junctions) {
        Add-Content tmp\acl-tmp.dat "$($junction[0].Substring(3))`r`n$($junction[2])"
    }
    
    # generate formatted acl file
    $aclTmp = [io.file]::ReadAllBytes("tmp\acl-tmp.dat")
    $acl = New-Object Byte[] ($aclTmp.length*2)
    $offset = 0
    for ($i = 0; $i -lt $aclTmp.length; $i++) {
        $acl[$offset] = $aclTmp[$i]
        $acl[$offset+1] = 0x00
        $offset += 2
    }
    [io.file]::WriteAllBytes("tmp\acl.dat", $acl)
    
    
    # create links
    foreach ($symlink in $symlinks) {
        cmd /c mklink /D "$($symlink[0])" "$($symlink[1])"
        cmd /c attrib +s +h +i "$($symlink[0])" /L
        cmd /c icacls "$($symlink[0])" /setowner SYSTEM /L
    }
    foreach ($junction in $junctions) {
        cmd /c mklink /J "$($junction[0])" "$($junction[1])"
        cmd /c attrib +s +h +i "$($junction[0])" /L
        cmd /c icacls "$($junction[0])" /setowner SYSTEM /L
    }
    
    # restore link permissions
    cmd /c icacls C:\ /restore tmp/acl.dat
    
    # cleanup tmp folder
    Remove-Item -Recurse -Force tmp
    

    In case anyone has any strange need to delete all the generated symlink and junction points, you can use this.

    @echo off
    
    set FILES=(^
        "C:\Users\All Users"^
        "C:\Users\Default User"^
        "C:\Users\Default\Application Data"^
        "C:\Users\Default\Cookies"^
        "C:\Users\Default\Local Settings"^
        "C:\Users\Default\My Documents"^
        "C:\Users\Default\NetHood"^
        "C:\Users\Default\PrintHood"^
        "C:\Users\Default\Recent"^
        "C:\Users\Default\SendTo"^
        "C:\Users\Default\Start Menu"^
        "C:\Users\Default\Templates"^
        "C:\Users\Default\AppData\Local\Application Data"^
        "C:\Users\Default\AppData\Local\History"^
        "C:\Users\Default\AppData\Local\Temporary Internet Files"^
        "C:\Users\Default\Documents\My Music"^
        "C:\Users\Default\Documents\My Pictures"^
        "C:\Users\Default\Documents\My Videos"^
        "C:\Users\Public\Documents\My Music"^
        "C:\Users\Public\Documents\My Pictures"^
        "C:\Users\Public\Documents\My Videos"^
        "C:\Users\%username%\Application Data"^
        "C:\Users\%username%\Cookies"^
        "C:\Users\%username%\Local Settings"^
        "C:\Users\%username%\My Documents"^
        "C:\Users\%username%\NetHood"^
        "C:\Users\%username%\PrintHood"^
        "C:\Users\%username%\Recent"^
        "C:\Users\%username%\SendTo"^
        "C:\Users\%username%\Start Menu"^
        "C:\Users\%username%\Templates"^
        "C:\Users\%username%\AppData\Local\Application Data"^
        "C:\Users\%username%\AppData\Local\History"^
        "C:\Users\%username%\AppData\Local\Temporary Internet Files"^
        "C:\Users\%username%\Documents\My Music"^
        "C:\Users\%username%\Documents\My Pictures"^
        "C:\Users\%username%\Documents\My Videos"^
    )
    
    for %%a in %FILES% do (
        icacls %%a /grant "%username%:(F)" /L
        icacls %%a /setowner %username% /L
        attrib -S -I -H %%a /L
        rmdir %%a
    )