Creating Websites using PowerShell scripts

Creating Website using Powershell scripts will take very less time, if you are planning to configure the multiple websites then this will be the best practices as it will take only few seconds to create an Website.

Follow the below steps to create the Website with Apppool using PowerShell scripts

Below is the new IIS Server which doesn’t have any website or app pool.

You can execute the PowerShell Scripts in 2 ways through Powershell ISE and by making it .ps1 executable file

Creating using an Poweshell ISE

Step 1 : Click Start > ‘Windows PowerShell ISE’ > right click > More > ‘Run as administrator’

Step 2 : Paste the below script in Powershell ISE and Click on Play Button

Import-Module WebAdministration
$iisAppPoolName = “mysite”
$iisAppPoolDotNetVersion = “v4.0”
$iisAppName = “mysite”
$directoryPath = “d:\mysite”

#navigate to the app pools root
cd IIS:\AppPools\

#check if the app pool exists
if (!(Test-Path $iisAppPoolName -pathType container))
{
    #create the app pool
    $appPool = New-Item $iisAppPoolName
    $appPool | Set-ItemProperty -Name “managedRuntimeVersion” -Value $iisAppPoolDotNetVersion
    $appPool | Set-ItemProperty -Name “enable32BitAppOnWin64” -Value “FALSE”           
    $appPool | Set-ItemProperty -Name “processModel.loadUserProfile”
  -Value “TRUE”
    $group = [ADSI]”WinNT://ComputerName/
IIS_IUSRS,group”
    $ntAccount = New-Object System.Security.Principal.
NTAccount(“IIS APPPOOL\$iisAppPoolName”)
    $strSID = $ntAccount.Translate([System.
Security.Principal.
SecurityIdentifier])
    $user = [ADSI]”WinNT://$strSID”
    $group.Add($user.Path)
    Write-Host “$iisAppPoolName : Pool added to IIS_IUSRS group successfully.” -foregroundcolor “green”              
}
#navigate to the sites root
cd IIS:\Sites\
#check if the site exists
if (Test-Path $iisAppName -pathType container)
{
    return
}
#create the site
$port = 443
$iisApp = New-Item $iisAppName -bindings @{protocol=”https”;
bindingInformation=”:443:”} -physicalPath $directoryPath
Write-Output $iisApp

$iisApp | Set-ItemProperty -Name “applicationPool” -Value $iisAppPoolName

Same script you can save as .ps1 and execute it on which ever IIS server

To install certificate on the website please refer Using PowerShell, Installing the Certificate for the Website and providing the required permission to access the website

TO Configure the Web server on your Windows Server please refer Configuring IIS Server on Windows Server 2016 Datacenter – GUI

If you face any issue comment below, we will help you to resolve it As soon As Possible….

Leave a Reply

Your email address will not be published. Required fields are marked *