Automating File Transfers Between OneDrive Accounts Using PowerShell

Managing files between multiple OneDrive accounts can be tedious, especially if you frequently need to transfer files from one account to another. You can automate this process using tools like Power Automate or PowerShell. Power Automate provides a user-friendly interface for creating automated workflows, while PowerShell offers a more script-driven approach for advanced users. This guide focuses on using PowerShell combined with Windows Task Scheduler for seamless automation every time you start your computer.


Prerequisites

Before starting, ensure the following:

  1. Both OneDrive accounts are synced to your computer via the OneDrive client.
  2. You know the local folder paths for the synced OneDrive libraries.
  3. PowerShell is installed on your system (it is included by default in Windows).
  4. You have administrative privileges on your machine.

Creating the PowerShell Script

  1. Open a text editor (e.g., Notepad).
  2. Paste the following PowerShell code into the editor:
# Variables
$sourceFolder = "C:\Users\YourUsername\OneDrive - SourceFolder" # Path to the source OneDrive folder
$destinationFolder = "C:\Users\YourUsername\OneDrive - DestinationFolder" # Path to the destination OneDrive folder

# Function to move files
function Move-OneDriveFiles {
    param (
        [string]$source,
        [string]$destination
    )

    if (-Not (Test-Path $source)) {
        Write-Host "The source folder $source does not exist!" -ForegroundColor Red
        return
    }

    if (-Not (Test-Path $destination)) {
        Write-Host "The destination folder $destination does not exist. Creating it now..." -ForegroundColor Yellow
        New-Item -ItemType Directory -Path $destination
    }

    # Move files and preserve folder structure
    Get-ChildItem -Path $source -Recurse | ForEach-Object {
        $destPath = Join-Path $destination $_.FullName.Substring($source.Length).TrimStart("\")
        if ($_.PSIsContainer) {
            if (-Not (Test-Path $destPath)) {
                New-Item -ItemType Directory -Path $destPath
            }
        } else {
            Move-Item -Path $_.FullName -Destination $destPath -Force
        }
    }

    Write-Host "File transfer completed." -ForegroundColor Green
}

# Execute the function
Move-OneDriveFiles -source $sourceFolder -destination $destinationFolder
  1. Replace YourUsername, SourceFolder, and DestinationFolder with your specific paths.
  2. Save the file with a .ps1 extension, such as Move-OneDriveFiles.ps1. Place it in a directory, e.g., C:\Scripts\.

Running the Script Automatically on Startup

To ensure the script runs every time your computer starts, configure it as a scheduled task in Windows Task Scheduler.

Steps to Configure the Task Scheduler

  1. Open Task Scheduler:
    • Press Win + R, type taskschd.msc, and press Enter.
  2. Create a New Task:
    • In the Task Scheduler, click Create Task in the right-hand Actions pane.
  3. Set General Properties:
    • In the General tab, give your task a name (e.g., Move OneDrive Files).
    • Check the option Run with highest privileges.
    • Under “Configure for,” select your Windows version (e.g., Windows 10).
  4. Set a Trigger:
    • Go to the Triggers tab and click New.
    • Set the trigger to At startup.
    • Click OK to save.
  5. Set an Action:
    • Go to the Actions tab and click New.
    • Choose Start a program.
    • In the “Program/script” field, type:powershell.exe
    • In the “Add arguments” field, enter:-ExecutionPolicy Bypass -File "C:\Scripts\Move-OneDriveFiles.ps1"
    • Click OK to save.
  6. Adjust Conditions (Optional):
    • In the Conditions tab, you can uncheck “Start the task only if the computer is on AC power” if using a laptop and you want the script to run on battery.
  7. Save the Task:
    • Click OK to save your task.

Testing Your Configuration

To ensure everything is working as expected:

  1. Manually run the task in Task Scheduler:
    • Select the task, right-click, and choose Run.
    • Check the OneDrive folders to confirm the files were moved.
  2. Restart your computer to verify the task runs at startup.

Key Considerations

  • File Overwrites: The script uses the -Force parameter to overwrite files in the destination folder if they already exist. Remove this flag if overwriting is not desired.
  • OneDrive Sync: Ensure both OneDrive accounts are fully synced before running the script to avoid missing files.
  • Debugging: If the script or task doesn’t work as expected, check the Task Scheduler logs in the History tab.

By automating the file transfer process, you can save time and ensure consistent updates between OneDrive accounts. This solution is flexible and can be adapted to suit other file management tasks.

Leave a comment