[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 Synced OneDrive, it remains un-synced and lacks a backup.

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

Do you need help with your Modern Workplace? Check out our service page!

$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
}
  

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, either with a webhook or RMM alert.

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 *