[PS SCRIPT]: Detecting Unlinked OneDrive SharePoint Document Libraries on Endpoints

What happens with data that exists only locally on a Device? When users manually create a folder on their device, within the root directory of the SharePoint OneDrive Folder, it exists only locally, it’s un-synced and lacks the cloud functionality and benefits of SharePoint including file versioning and backups.

I was unable to prevent users from creating folders. However, I was able to create a detection script, which can prevent users from losing important company data.

The script can be run as system, as it loops through all user profiles.

$onedrives = Get-ChildItem "C:\Users\*\OneDrive -*"
$alert = $false

$DIAG =@()

foreach ($onedrive in $onedrives) {
    $companyname = $onedrive.Name.TrimStart("OneDrive - ")
    Write-Output "Looping $($onedrive)"
    
    $children = get-childitem "$($onedrive.PSParentPath)\$companyname"

    foreach ($child in $children) {
        if (!($child.Mode.Contains("l"))) {
            $DIAG += "Unlinked folder detected: $($child.FullName)"
            $alert = $true
        }
    }
}

if ($alert -eq $true) {
    write-output $DIAG
    write-output "SharePoint unlinked folders detected"

    #Add some alert action here
    exit 1
}
  Code language: PHP (php)

When executed, it loops through the OneDrive folders for all users, grabs the company name, and checks if the folders are linked. If it detects an unlinked folder, it exits with code 1.

“C:\Users\JeremyPot\OneDrive – Prof-IT.Services”
Is translated to:
“C:\Users\JeremyPot\Prof-IT.Services”

Of course, you do want to change the script to suit your environment, so you are alerted when issues occur.

Don’t miss out on this related post! [PS SCRIPT]: OneDrive Documents Redirection and Status check – Prof-IT Services

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *