Windows 10 – Batch – Create a Folder Junction (also OneDrive)

email me

The NTFS file system provides the ability to create a system representation of a file or directory in a location in the directory structure that is different from the file or directory object that is being linked to. This process is called linking. There are two types of links supported in the NTFS file system: hard links and junctions.

A hard link is the file system representation of a file by which more than one path references a single file in the same volume.

junction (also called a soft link) differs from a hard link in that the storage objects it references are separate directories, and a junction can link directories located on different local volumes on the same computer. Otherwise, junctions operate identically to hard links. Junctions are implemented through reparse points.  more…

Basically, a junction just connects a source folder to a new target location…and Mklink is what makes the magic happen.


Example 1

Soft linking a Test folder and the Desktop folder

:: removing read only attribute
attrib "%userprofile%\Test" -r

:: deleting folder
rd /Q /s "%userprofile%\Test"

:: making link
mklink /j "%userprofile%\Test" "C:\users\%username%\Desktop"

 

Example 2

Soft linking C: drive profile\Desktop to D: drive profile\Desktop

attrib "%userprofile%\Desktop" -r
rd /Q /s "%userprofile%\Desktop"
mklink /j "%userprofile%\Desktop" D:\users\%Profile%\Desktop

 

 

Notes

Hard Links and Junctions

Registry Settings for Folder Redirection in Windows

Folder Redirection, Offline Files, and Roaming User Profiles overview

 

Registry

Reg keys for User Shell Folder redirection

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
Name: AppData
Type: REG_EXPAND_SZ
Data: %USERPROFILE%\Application Data

Name: Desktop
Type: REG_EXPAND_SZ
Data: %USERPROFILE%\Desktop

Name: Personal
Type: REG_EXPAND_SZ
Data: %USERPROFILE%\My Documents

Name: My Pictures
Type: REG_EXPAND_SZ
Data: %USERPROFILE%\My Documents\My Pictures

Name: Start Menu
Type: REG_EXPAND_SZ
Data: %USERPROFILE%\Start Menu

 

Cloud

OneDrive Guide for Enterprises

Deploy OneDrive apps by using System Center Configuration Manager

Deploy OneDrive apps by using Intune

Network utilization planning for the OneDrive sync app

Redirect and move Windows known folders to OneDrive

Deploy and configure the new OneDrive sync app for Mac

 

OneDrive Group Policy

Use Group Policy to control OneDrive sync client settings

 

Manually Testing

1. Move the contents of the user’s profile folder (example: Documents) to the OneDrive cloud storage sync location on their local hard drive: %userprofile%\Documents to
$userprofile%\OneDrive\Documents.

2. Create a GPO to redirect the Documents folder from %userprofile%\Documents to
%userprofile%\OneDrive\Documents. This can be done using either Folder Redirection policy, or a registry mod.

3. Ensure that the OneDrive sync client is installed on other enterprise computers.

4. When the user logs in for the first time on a new computer, they will authenticate to OneDrive.

 

Automated Redirect Solution

* Using the Desktop folder as an example…

Move Data
robocopy /MOVE /E /PURGE “%userprofile%\Desktop” “%userprofile%\OneDrive\Desktop”

Make Link
mklink /D %userprofile%\Desktop %userprofile%\OneDrive\Desktop

Update User Shell Folder
reg add “HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders” /v Desktop /t REG_EXPAND_SZ /d ^%USERPROFILE^%”\OneDrive\Desktop” /f

Reboot
reboot, or reload explorer.exe

 

tags: Junctions scripting, linking automation, folder redirection, OneDrive redirect, OneDrive automation, MrNetTek