PowerShell – Create new website


PowerShell Create new website

Because I’m lazy and/or I don’t have time to format the code, above is a screenshot for clarity, below is the code for whoever needs it.

function New-SimpleWebsite($siteName, $port, $webroot)
    Import-Module WebAdministration

    $IISPath = “IIS:\Sites\$siteName”
    $BindingsArgs = ‘@{protocol=””http””;bindingInformation=””*:’+ $port +’:””}’
    “`nIISPath = $IISPath”
    “`nBindingsArgs = $BindingsArgs”
    if (Test-Path $IISPath) {
        Write-Host “Web site $siteName already exists !”
        Write-Host “$siteName creation…”
        $webroot = $webroot.Replace(“\\”, “\”)
        New-Item $IISPath -bindings $BindingsArgs -physicalPath $webroot
        Start-Website -Name $siteName
    Set-WebConfigurationProperty -filter /system.webServer/directoryBrowse -name enabled -value true -PSPath “IIS:\Sites\$siteName”

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


Fully qualified absolute URL in C#


Uri baseUri = new Uri(Request.Url.Scheme +
    Uri.SchemeDelimiter +
    Request.Url.Host +
    (Request.Url.IsDefaultPort ?
        ”” :
        ”:” +
finalUrl = new Uri(baseUri, innerUrl.Replace(“~/”, “”)).AbsoluteUri;


finalUrl = Request.Url.GetLeftPart(UriPartial.Authority) +
    VirtualPathUtility.ToAbsolute(“~/”) +
    innerUrl.Replace(“~/”, “”);

%d bloggers like this: