title: Creating virtual hosts for wampserver with powershell
body: <p>I am trying to create virtual hosts with a Powershell script. It works fine, but I am having trouble with the encoding. I have tried all the <code>-encoding</code> switch values allowed with no luck.</p>
  
  <p>Here is my script</p>
  
  <pre class="lang-vb prettyprint-override"><code>$hostName = $args[0];
  if ($args -eq $null -or $args.Length -eq 0)
  {
      "No arguments. Supply the virtual host name as the first argument."
      exit
  }
  if (!(Test-Path -path D:\wamp\$hostName))
  {
      $indexText = "&lt;?php echo `"&lt;h1&gt;Holder for $hostName&lt;/h1&gt;`";"
      New-Item D:\wamp\$hostName -type directory
      New-Item D:\wamp\$hostName\index.php -type file
      Out-File -FilePath D:\wamp\$hostName\index.php -InputObject $indexText -Encoding UTF8
  }
  $hosts = Get-Content C:\Windows\System32\drivers\etc\hosts
  $hosts = $hosts + "127.0.0.1 $hostName.localhost"
  Out-File -FilePath C:\Windows\System32\drivers\etc\hosts -InputObject $hosts
  $conf = Get-Content "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf"
  $conf = $conf + "&lt;VirtualHost *:80&gt;"
  $conf = $conf + "    DocumentRoot `"D:/wamp/$hostName`""
  $conf = $conf + "    ServerName $hostName.localhost"
  $conf = $conf + "    ErrorLog `"logs/$hostName.localhost-error.log`""
  $conf = $conf + "    CustomLog `"logs/$hostName.localhost-access.log`" common"
  $conf = $conf + "&lt;/VirtualHost&gt;"
  Out-File -FilePath "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf" -InputObject $conf -Encoding ascii
  Write-Host "Restart wamp server to effect changes"
  </code></pre>
  
  <p>Has anybody managed to get this working reliably or am I on to a none-starter here? I can always use PHP instead :)</p>
  
  <p>If the encoding is not correct then Apache refuses to start under wampserver. If I change the encoding in notepad++ to ANSI then everything works fine, but powershell doesn't like <code>-encoding ANSI</code>.</p>