I have a need to update the DOCX file content using SharePoint 2013 PowerShell. I am using below code to do the same, but once file gets updated and checked in by the code, the files getting corrupted and don't open in word application. So, please let me know if anyone have done such requirement.
foreach ($file in $folder.Files){ Write-Host $file.Name Write-Host $file.CheckOutType $file.CheckOut() $BinaryContent= $file.OpenBinary() $encode = New-Object System.Text.ASCIIEncoding $ContentString = $encode.GetString($BinaryContent) Write-Host -BackgroundColor Cyan $ContentString $ContentString = $ContentString -replace "kailash","KCS" $file.SaveBinary($encode.GetBytes($ContentString)) $file.CheckIn("modified by code") }
Any help is appreciated.Thanks in advance!