Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6532723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:02:20+00:00 2026-05-25T10:02:20+00:00

I am trying to create virtual hosts with a Powershell script. It works fine,

  • 0

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 -encoding switch values allowed with no luck.

Here is my script

$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 = "<?php echo `"<h1>Holder for $hostName</h1>`";"
    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 + "<VirtualHost *:80>"
$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 + "</VirtualHost>"
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"

Has anybody managed to get this working reliably or am I on to a none-starter here? I can always use PHP instead 🙂

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 -encoding ANSI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:02:21+00:00Added an answer on May 25, 2026 at 10:02 am

    using the -Encoding OEM option on Out-File seems to be a solution to this problem incase anybody else comes up against it.

    For example:-

    Out-File -FilePath "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf" -InputObject $conf -Encoding OEM
    

    Edit:-
    I eventually solved this problem by rewriting the script in PHP as I could never get it to work consistently in Powershell. Here is my script incase others find it useful, it is designed to run from the command line eg:-

    php d:/scripts/newVHost.php www
    

    Will create a new virtual host call ‘www’.

    <?php
    /**
     * create a new vhost on this server
     * 
     * @param host name
     */
    $ds = DIRECTORY_SEPARATOR;
    $eol = PHP_EOL;
    
    // you may need to alter this if your host file is elsewhere
    $hostsFile = "C:\Windows\System32\drivers\etc\hosts";
    $hostFH = fopen($hostsFile, 'a+');
    
    // you may need to alter this if your htttp-vhosts.conf file is elsewhere
    $apacheVHfile = "C:\Program Files\wamp\bin\apache\Apache2.2.17\conf\extra\httpd-vhosts.conf";
    $apacheFH = fopen($apacheVHfile, 'a+');
    
    if($argc <= 1) die("You must specify a host name\n");
    $hostName = $argv[1];
    $pathName = "d:{$ds}www{$ds}$hostName";
    echo "\nCreating new virtual host '$hostName'\n";
    
    if(!file_exists($pathName)) mkdir($pathName);
    
    $writeStr = "\n127.0.0.1       $hostName.localhost";
    writeFile($hostFH, $writeStr, $hostsFile);
    
    $writeStr = "<VirtualHost *:80>
        DocumentRoot \"$pathName\"
        ServerName $hostName.localhost
        ErrorLog \"logs{$ds}$hostName.localhost-error.log\"
        CustomLog \"logs{$ds}$hostName.localhost-access.log\" common
    </VirtualHost>$eol";
    
    writeFile($apacheFH, $writeStr, $apacheVHfile);
    
    fclose($hostFH);
    fclose($apacheFH);
    
    echo "New virtual host $hostName created. Restart WAMP server\n";
        echo "\n===================================\n\n";
    
    function writeFile($file, $writeStr, $fileName = null){
        $written = fwrite($file, $writeStr);
        if($written){
            echo "\nModified $fileName\n";
        } else echo "\nCould not Modify $fileName\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create virtual desktop. I complete successfully.But i am not able
I'm trying to create a virtual wiki but I already struggle with In your
I am trying to create a list of all virtual directories within an IIS
I have a Virtual Private server with Dreamhost. I'm trying to create a shared
I am trying to create a virtual directory and set it's permissions using IIS7
help me to manage IIS (what i am trying to do is create virtual
Ok so I am trying create a login script, here I am using PHP5
Trying to create a user account in a test. But getting a Object reference
I'm trying to execute iisvdir from an ant script to clean and create a
How can I create a virtual HID device in code? I'm trying to avoid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.