Was recently thrown with a challenge during a Windows 10 migration project for a client to set certain folders in their OneDrive for Business to be always available offline, once user’s data has been successfully migrated (to OneDrive) from on premise file server.
For the benefits of everyone, latest version of OneDrive for Business client comes with improved features that allows you to either set files / folders to be always available offline or online (to free up spaces of your local storage)
While this option is available out of the box when the OneDrive sync client has been successfully setup, how do we further automate it so that certain files / folder can be set to “Always keep on this device (available offline)” without user’s intervention ?
Referring to the details online, we can create a PowerShell script using the attrib.exe, but based on my testing, “attrib -U +P /s” sets all the files / subfolder of the target folder you performed the command at to be available offline but not the parent folder.
To fix this, you can include a /D switch, as in “attrib -U +P /s /D”.
I’ve included a humble script of mine to apply the settings when the folder exists and the settings had not been applied.
$targetFolder = "<Target Folder>"
$rootDir = $env:userprofile + "\<your OneDrive Path>"
$FolderPath = $rootDir + "\" + $targetFolder
if (test-path $FolderPath)
{
set-location $rootDir
$Attrib = attrib $targetFolder
$Attrib = $Attrib.trim()
$arrayAttrib = $Attrib.Split("C:")
$fileAttib = $arrayAttrib[0]
$fileAttib = $fileAttib.TrimStart()
$fileAttib = $fileAttib.TrimEnd()
if ($fileAttib -ne 'P')
{
Set-Location $FolderPath
attrib.exe +P -U /S /D
Set-Location $rootDir
attrib.exe +P -U /S /D $targetFolder
}
}
Linking this script to a GPO logon script will make sure that the desired folder is always set available offline when user logs into their machine.
Enjoy…