title: How to run appCmd in PowerShell to add custom headers to the Default Web Site
body: <p>Please help me figure out how to properly escape the arguments so they work when calling appcmd inside of powershell.</p>
  
  <p><strong>My script looks like this:</strong></p>
  
  <pre><code>$defaultWebSite = "Default Web Site"
  $appCmd = "C:\windows\system32\inetsrv\appcmd.exe"
  $addHeaderP3P = "set config ""$defaultWebSite"" -section:system.webServer/httpProtocol /+""customHeaders.[name='P3P',value='policyRef=`\`"/w3c/p3p.xml`\`",CP=`\`"DSP COR NID OUR COM PRE`\`"']`""
  
  Write-Output "Here's the argument string: " $addHeaderP3P
  
  
  
  Write-Output "`nInvoke Result:"
  Invoke-Expression "$appCmd $addHeaderP3P"
  
  
  Write-Output "`n&amp; Result:"
  &amp; $appCmd --%"$addHeaderP3P"
  </code></pre>
  
  <p><strong>The output is this when running inside powershell_ise:</strong></p>
  
  <pre><code>PS C:\Users\robert.bratton&gt; D:\Junk\p3pheader.ps1
  Here's the argument string: 
  set config "Default Web Site" -section:system.webServer/httpProtocol /+"customHeaders.[name='P3P',value='policyRef=\"/w3c/p3p.xml\",CP=\"DSP COR NID OUR COM PRE\"']"
  
  Invoke Result:
  Failed to process input: The parameter 'COR' must begin with a / or - (HRESULT=80070057).
  
  
  &amp; Result:
  Failed to process input: The parameter 'NID' must begin with a / or - (HRESULT=80070057).
  </code></pre>
  
  <p><strong>This works from the command line</strong></p>
  
  <pre><code>"C:\windows\system32\inetsrv\appcmd.exe" set config "Default Web Site" -section:system.webServer/httpProtocol /+"customHeaders.[name='P3P',value='policyRef=\"/w3c/p3p.xml\",CP=\"DSP COR NID OUR COM PRE\"']"
  </code></pre>
  
  <p>Thanks for your help!</p>