Bulk file rename to remove invalid characters

Modified on Thu, 11 Aug 2016 at 05:04 PM

*** Before you step into the world of command-line tools, try the automated tool designed for end-users here ***


OK, so if you can run PowerShell as Administrator then you can probably make use of this script to remove invalid characters from a User's existing files. This will allow the files to sync to OneDrive for Business without it complaining! Note: does not fix error caused by long filenames.


Details

Microsoft list a number of invalid characters for filenames (See https://support.office.com/en-gb/article/Invalid-characters-in-file-or-folder-names-or-invalid-file-types-in-OneDrive-for-Business-64883A5D-228E-48F5-B3D2-EB39E07630FA?ui=en-US&rs=en-US&ad=US) including:


"  #  %  *  :  <  >  ?  /  \  |  &  {  }  ~


The script below will automatically remove the following characters: &  {  }  ~  #  %


Two steps solution:

Copy & Paste the large script from the bottom of this article into a PowerShell console (run "as Administrator"), and tap Enter (this loads the script into the current session, ready for use)

Next, copy the path of the User's OneDrive for Business folder into the following script, and Paste+Enter within PowerShell:

 

Check-IllegalCharacters -Path "C:\Users\YourUsersNameHere\OneDrive - YourCompanyNameHere\" -Fix

 

This will churn through all of the User's files and output to the screen any files that it fixed by renaming. This should save you a lot of time, and did not require the purchase of any special software.

[End of article]


(The large script now follows)


 

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
        {
            #Uses RegEx
            $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
            }
        }
    }
}

 

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article