# M365-OneDriveForBusiness-User-FileDownload.ps1

# Table of Contents

  • Table of Contents
  • Purpose
  • Dependencies
  • Function
  • Usage
  • Inputs
  • Variables
  • Command Line Flags
  • Outputs
  • Notes

# Purpose

Downloads all files from a user's OneDrive for Business folder to a local destination, recursively, preserving structure and metadata. Supports logging, zipping, and cleanup.

# Dependencies

# Function

  1. Prompts for or accepts parameters: tenant, username, origin folder, destination folder, log file, and zip/delete options.
  2. Validates and creates the destination folder if needed.
  3. Connects to the user's OneDrive for Business using PnP.PowerShell (interactive authentication).
  4. Validates the remote folder exists, prompting the user if not.
  5. Builds a set of already-downloaded files from previous logs to avoid re-downloading.
  6. Recursively downloads all files and folders, preserving structure and metadata.
  7. Optionally compresses the download to a .zip and deletes the original files if requested.
  8. Logs all actions, errors, and a summary to a log file and the console.

# Usage

./M365-OneDriveForBusiness-User-FileDownload.ps1 -TenantID "contoso" -TargetUsername "john_smith_contoso_com" -DestinationFolder "C:\Backup" [-OriginFolder "/Documents"] [-LogFile "C:\Temp\OneDriveBulkDownload_20250908.txt"] [-ZipAfterDownload] [-DeleteAfterZip]

If parameters are omitted, the script will prompt for them interactively.

# Inputs

  • -TenantID (string, required): The tenant short name (e.g. contoso).
  • -TargetUsername (string, required): The OneDrive username (e.g. john_smith_contoso_com).
  • -OriginFolder (string, optional): The OneDrive/SharePoint folder to download from (default: /Documents).
  • -DestinationFolder (string, required): The local folder to download to.
  • -LogFile (string, optional): Path to the log file (default: temp directory).
  • -ZipAfterDownload (switch, optional): Compress the download folder to a .zip file after download.
  • -DeleteAfterZip (switch, optional): Delete the downloaded files after creating the .zip.

# Variables

  • $TenantID, $TargetUsername, $OriginFolder, $DestinationFolder, $LogFile, $ZipAfterDownload, $DeleteAfterZip: Script parameters.
  • $SiteUrl: The constructed OneDrive site URL.
  • $downloadedFilesSet: Tracks files already downloaded in previous sessions.
  • $alreadyDownloaded: Counter for skipped files.

# Command Line Flags

  • All parameters above can be used as flags.

# Outputs

  • Downloads all files and folders from the specified OneDrive location to the local destination.
  • Logs all actions, errors, and a summary to the specified log file and the console.
  • Optionally creates a .zip archive and deletes the original files if requested.

# Notes

  • Requires PowerShell 7 and PnP.PowerShell module.
  • User must have permission to access the target OneDrive.
  • All output is accessible and uses EN-AU spelling.
  • See project standards for robust root-based module import and error handling.