When you try to upload multiple files in SharePoint using a PowerShell script, you may end up with script not uploading certain files. The reason is the file has the special characters in its name. First you need to remove all the special characters in the file name before uploading it.
The following powershell script is used to replace special characters in file name,
- $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
- $LogFile = ".\ReplaceSpecialCharactersInFileNamePatch-$LogTime.rtf"
- # Add SharePoint PowerShell Snapin
-
- if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
- Add-PSSnapin Microsoft.SharePoint.Powershell
- }
- start-transcript $logfile
- $path = read-host "Enter the path"
- $TestPath = test-path $path
- if($TestPath)
- {
- Get-ChildItem -path $path -recurse |
- Foreach-Object {
- $newName = $_.name -replace '[~#%&*{}|:<>?/|"]', ''
- if (-not ($_.name -eq $newname)){
- Rename-Item -Path $_.fullname -newname ($newName)
- }
- }
- }
- else
- {
- write-host "Invalid path details.... Please run the script again and enter the valid path" -fore cyan
- write-host "SCRIPT FINISHED" -fore yellow
- }
-
- stop-transcript