PowerShell: Uploading (Migrating) files to OneDrive using PowerShell

Copying files into OneDrive for as an individual is easy. If you're responsible for migrating 500 users into OneDrive it is not...

This PowerShell script will allow you to upload files into a users OneDrive or if you're really clever could be deployed to upload lots and lots of users into one drive.

$SourceUsername = "You_User" <#Username. Remember to replace . with _#>
$SourceFolders = "C:\temp" <#The local location of files#>
$targetlocation = "Documents/temp" <#The destination#>

$junk = "Microsoft.PowerShell.Core\FileSystem::"
$SourceUrl ="https://company-my.sharepoint.com/personal/"+ $SourceUsername + "_company-group_com/"

$SourceSPconnect = Connect-PnPOnline –Url $SourceUrl  -UseWebLogin -ReturnConnection

$Directorys = Get-ChildItem -Path $SourceFolders -Recurse -Directory -Force -ErrorAction SilentlyContinue | select *
$Files = Get-ChildItem -Path $SourceFolders -Recurse -File -Force -ErrorAction SilentlyContinue | select *

foreach ($Directory in $Directorys)
{
 $TruePath = $SourceUrl + $targetlocation
 $onePath=($directory.PSParentPath).Replace($SourceFolders,"")
 $onePath=($onePath).Replace($junk,"")
 $onePath=$onePath.Replace('\','/')
 $TruePath = $TruePath + $onePath

 Add-PnPFolder -Name $Directory.Name -Folder $TruePath 
}

foreach ($File in $Files)
{
$Filepath = $File.PSParentPath.Replace($SourceFolders,"")
$Filepath = $Filepath.Replace($junk,"")
$Filepath = $Filepath.Replace('\','/')
$Filepath = $targetlocation + $Filepath
Add-PnPFile -Path $file.Fullname -Folder $Filepath
}