title: Powershell Invoke-WebRequest causing server error
body: <p>I have an xml file and I'm using a Powershell script to create a soap request to post it to the url.</p>
  
  <p>The problem I seem to have is with this line of code below producing a server error: </p>
  
  <pre><code>[xml]$res = Invoke-WebRequest $uri -Method POST -ContentType "text/soap+xml;charset=windows-1252" -Body $soapReq
  </code></pre>
  
  <p>The odd thing is when I open my data XML file in notepad and save it (make no changes), the Powershell script seems to work and transfer the file across to the target url.</p>
  
  <p>I'm writing my xml file in UTF8 charset.</p>
  
  <p>I enclose a tweaked version of Powershell script I am using.</p>
  
  <pre><code>#my db generates this file
  $filePath = "C:\MyPath\data.xml"
  
  $result = Get-Content $filePath
  
  #rewrite the file to a specific location and encode it to utf-8
  Out-File -FilePath $filePath -InputObject $result -Encoding 'UTF8'
  
  $un = 'un'
  $Pw = 'pw'
  
  $xmlData = Get-Content $filePath -Raw
  $xmlData = [Security.SecurityElement]::Escape($xmlData)
  
  $soapReq = @"
  &lt;?xml version="1.0" encoding="Windows-1252"?&gt;
  &lt;env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="https://url"&gt;
  &lt;env:Body&gt;
    &lt;ins0:Transfer&gt;
      &lt;sourceIdentifier&gt;123456&lt;/sourceIdentifier&gt;
      &lt;userName&gt;$un&lt;/userName&gt;
      &lt;password&gt;$pw&lt;/password&gt;
      &lt;xmlDataset&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
  $xmlData&lt;/xmlDataset&gt;
    &lt;/ins0:Transfer&gt;
  &lt;/env:Body&gt;
  &lt;/env:Envelope&gt;
  "@
  
  $uri = "https://url"
  
  [xml]$res = Invoke-WebRequest $uri -Method POST -ContentType "text/soap+xml;charset=windows-1252" -Body $soapReq
  </code></pre>
  
  <p>Before I investigate using vbs to open the file in notepad and save it (overkill), I’m hoping someone can give me advice on how to get this Powershell script working properly.</p>