Office 365/SharePoint Online – OneDrive File Migration


Currently I am in the process of migrating a file share to SharePoint online libraries. One of the pre-check that I need to run before synchronizing the files is to make sure that these files/folders don’t have illegal characters defined by SharePoint online requirement.

The script below

function Check-IllegalCharacters ($Path, [switch]$Fix, [switch]$Verbose) { Write-Host Checking files in $Path, please wait... #Get all files and folders under the path specified $items = Get-ChildItem -Path $Path -Recurse foreach ($item in $items) { #Check if the item is a file or a folder if ($item.PSIsContainer) { $type = "Folder" } else { $type = "File" } #Report item has been found if verbose mode is selected if ($Verbose) { Write-Host Found a $type called $item.FullName } #Check if item name is 128 characters or more in length if ($item.Name.Length -gt 127) { Write-Host $type $item.Name is 128 characters or over and will need to be truncated -ForegroundColor Red } else { $illegalChars = '[&{}~#%]' filter Matches($illegalChars) { $item.Name | Select-String -AllMatches $illegalChars | Select-Object -ExpandProperty Matches Select-Object -ExpandProperty Values } #Replace illegal characters with legal characters where found $newFileName = $item.Name Matches $illegalChars | ForEach-Object { Write-Host $type $item.FullName has the illegal character $_.Value -ForegroundColor Red #These characters may be used on the file system but not SharePoint if ($_.Value -match "&") { $newFileName = ($newFileName -replace "&", "and") } if ($_.Value -match "{") { $newFileName = ($newFileName -replace "{", "(") } if ($_.Value -match "}") { $newFileName = ($newFileName -replace "}", ")") } if ($_.Value -match "~") { $newFileName = ($newFileName -replace "~", "-") } if ($_.Value -match "#") { $newFileName = ($newFileName -replace "#", "") } if ($_.Value -match "%") { $newFileName = ($newFileName -replace "%", "") } } #Check for start, end and double periods if ($newFileName.StartsWith(".")) { Write-Host $type $item.FullName starts with a period -ForegroundColor red } while ($newFileName.StartsWith(".")) { $newFileName = $newFileName.TrimStart(".") } if ($newFileName.EndsWith(".")) { Write-Host $type $item.FullName ends with a period -ForegroundColor Red } while ($newFileName.EndsWith(".")) { $newFileName = $newFileName.TrimEnd(".") } if ($newFileName.Contains("..")) { Write-Host $type $item.FullName contains double periods -ForegroundColor red } while ($newFileName.Contains("..")) { $newFileName = $newFileName.Replace("..", ".") } #Fix file and folder names if found and the Fix switch is specified if (($newFileName -ne $item.Name) -and ($Fix)) { Rename-Item $item.FullName -NewName ($newFileName) Write-Host $type $item.Name has been changed to $newFileName -ForegroundColor Blue } } } } $ErrorActionPreference="SilentlyContinue" Stop-Transcript | out-null $ErrorActionPreference = "Continue" Start-Transcript -path C:\LogFileLocation\IllegalCharsFound.txt -append # DO YOUR EXECUTION COMMAND HERE Check-IllegalCharacters -Path "D:\Egnyte\MCM" Stop-Transcript