# M365-OneDriveForBusiness-User-FileUpload.ps1

# Table of Contents

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

# Purpose

Bulk uploads files and folders from a local directory to a user's OneDrive for Business, with robust logging, error handling, metadata preservation, and flexible configuration. Supports dry run, interactive prompts, and advanced name handling.

# Dependencies

# Function

  1. Prompts for or accepts parameters: tenant, username, origin folder, destination folder, log file, dry run, and invalid name handling mode.
  2. Validates tenant and user, including OpenID and OneDrive URL checks.
  3. Allows browsing for the local folder interactively.
  4. Validates and sets the OneDrive destination folder (site-relative path).
  5. Ensures required modules are installed and imported.
  6. Connects to OneDrive for Business using PnP.PowerShell (interactive authentication).
  7. Recursively uploads all files and folders, preserving structure and metadata.
  8. Handles invalid file/folder names per user-selected mode (auto, prompt, skip).
  9. Skips or overwrites files based on name and size checks.
  10. Logs all actions, errors, and a summary to a log file and the console.
  11. Supports dry run mode to preview actions without uploading.

# Usage

./M365-OneDriveForBusiness-User-FileUpload.ps1
# or with parameters:
./M365-OneDriveForBusiness-User-FileUpload.ps1 -TenantID "contoso" -TargetUsername "john_smith_contoso_com" -OriginFolder "C:\Restore" -DestinationFolder "/Documents/Restore" [-LogFile "C:\Temp\OneDriveBulkUpload_20250908.txt"] [-DryRun]

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 or email.
  • -OriginFolder (string, required): Local folder to upload from.
  • -DestinationFolder (string, required): OneDrive/SharePoint folder to upload to (site-relative path).
  • -LogFile (string, optional): Path to the log file (default: temp directory).
  • -DryRun (switch, optional): Show what would be uploaded/skipped without making changes.
  • -InvalidNameMode (string, optional): How to handle invalid file/folder names (auto, prompt, skip).

# Variables

  • $TenantID, $TargetUsername, $OriginFolder, $DestinationFolder, $LogFile, $DryRun, $InvalidNameMode: Script parameters.
  • $SiteUrl, $siteRelativeRoot: Constructed URLs for OneDrive.
  • $TestMetadataMode, $ThrottleLimit: Advanced/test options.

# Command Line Flags

  • All parameters above can be used as flags.

# Outputs

  • Uploads all files and folders from the specified local directory to the user's OneDrive location.
  • Logs all actions, errors, and a summary to the specified log file and the console.
  • In dry run mode, shows what would be uploaded/skipped without making changes.

# 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.