powershellreplacebyte-order-mark

special characters being replaced by PowerShell with a question mark?


We've written a script to loop through all our files and remove a certain string. The string contains no special characters and the script does remove the string and replace it with nothing like we're after. But, there are some special characters in the files. Instead of removing just the string we specify and leaving everything else in the files as is, the script replaces these special characters with a question mark, "?" We've tried both the PowerShell -replace and .Replace functions with the same effect. Both replace this special character with "?"

this is the script we run:

$packageFiles = Get-ChildItem -Path E:\SSIS\Projects -Recurse -Include *.dtsx 
foreach ($file in $packageFiles)
{
    (Get-Content $file.PSPath) |
    Foreach-Object { $_.Replace('somestring','')} |
    Set-Content $file.PSPath
}

and this is what the line with the special character looks like in the file before:

enter image description here

and after:

enter image description here

The before has the non breaking space byte order mark special character, "ZWNBSP". And the after has replaced the special character with a question mark "?".

Does anyone have a solution that will allow us to still use one of PowerShell's replace functions but not have our special characters replaced by question marks which breaks our files?

UPDATE: The file we are trying to find/replace within is an SSIS package which is xml. I tried rewriting the script to use xml functions rather than treating the file as plain text. This is the new script:

$xmlPath = "E:\somepath\package.dtsx"
$xmlContent = Get-Content -Path $xmlPath
$xml = [xml]$xmlContent

$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("xml", "http://www.w3.org/XML/1998/namespace")
$ns.AddNamespace("DTS", "www.microsoft.com/SqlServer/Dts")

$pwd_xml = $xml.SelectSingleNode("//DTS:Variable[@DTS:ObjectName='SSIS_APP_PWD']/DTS:VariableValue", $ns)
$pwd_xml


$pwd_xml.innerText = ''

$xml.Save("E:\somepath\package.dtsx")

and instead of replacing the original special character with a question mark it now replaces it with "":

enter image description here

This still breaks our file.


Solution

  • Building on Santiago's helpful comments:

    tl;dr

    Your symptom implies that you're using Windows PowerShell, where Set-Content defaults to ANSI encoding, in which a character such as ZERO WIDTH NO-BREAK SPACE, U+FEFF can not be represented and is "lossily" translated to a verbatim ? character.

    Assuming that your input files are UTF-8 files with BOM, the solution is therefore to pass -Encoding UTF8 to Set-Content (streamlined version of your code):

    Get-ChildItem E:\SSIS\Projects -Recurse -Include *.dtsx |
      ForEach-Object {
        ($_ | Get-Content -Raw).Replace('somestring','') |
          Set-Content -LiteralPath $_.FullName
      }
    

    If there's also a problem in how the files are read, use an -Encoding argument with Get-Content too.

    As for the XML API-based approach you added later (which is preferable in this case):


    Background information