Friday, December 22, 2017

Can you create a new web IIS site on your computer using PowerShell?

Yes you can. PowerShell is indeed powerful. In order to create a new web site you need to import the webadministration module using this import-module statement. However, you should launch Powershell with administrator privileges.

Import-Module webadministration

After running the above and if there are no errors run the following statement to create a new website (TestSite)is this:

New-WebSite -Name TestSite -Port 80 -HostHeader TestSite -Physical Path "$env:systemdrive\inetpub\testsite"

This will spawn an error, if the folder testsite does not exist in the inetpub directory. Create the folder first and then run the New-Website command as above.
----------
New-WebSite : Parameter 'PhysicalPath' should point to existing path.
At line:3 char:1
+ New-WebSite -Name TestSite -Port 80 -HostHeader TestSite -PhysicalPat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-Website], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.IIs.PowerShell.Provider.NewWebsiteCommand

---------------------------
You can verify the TestSite you created in IIS Manager as shown.



IIS_TestSite

No comments: