sharepointdownloadonedrivewindows-scriptingfile-attributes

How to programmatically download a file from a synced SharePoint directory in Windows?


I synced a SharePoint directory to my Windows file system using OneDrive. As seen in the image below, some files are available locally, while others are only available in the cloud:

Screenshot

When I double-click on a file that's only available in the cloud, it gets downloaded automatically. However, I want to achieve the same result programmatically using the terminal (CMD).

Here’s what I tried:

  1. I navigated to the directory containing the file:

    cd "C:\Users\MyUsername\OneDrive\Documents"
    
  2. I attempted to use the attrib command to make the file available locally:

    attrib -P +U my_file.iso
    

    This did not produce any output, and the file remains only in the cloud.

  3. I also tried without the file extension:

    attrib -P +U my_file
    

    This resulted in an error:

    Could not find that file.
    

What I Need

I want to programmatically trigger the download of a specific file (e.g., my_file.iso) from the synced SharePoint directory to make it available locally, without manually double-clicking it.

Additional Details

What I’ve Tried So Far


Solution

  • I figured out I could use this simple PowerShell script:

    \$filePath = "${filePath.replace('/', '\\')}" 
    
    if (!(Test-Path \$filePath)) {
        throw "Could not find ISO file at \$filePath"
    }
    
    \$fileAttributes = (Get-Item \$filePath).Attributes
    if (\$fileAttributes -band [System.IO.FileAttributes]::Offline) {
        Write-Host "The file is only available in the cloud. Downloading..."
        Get-Content \$filePath > \$null
        Write-Host "The file has been successfully downloaded."
    } else {
        Write-Host "The file is already available locally."
    }