Unable to use Powershell 5.1 on Server 2019 to set IIS to require SSL and to redirect HTTP requests to HTTPS

Richard Duane Wolford Jr 206 Reputation points
2022-03-21T19:22:14.147+00:00

I need some help with Powershell 5.1 and IIS. First some info: I'm able to successfully deploy both my site (after deleting the default site), my certificate, and bind the certificate to the site. Easy peasy. BUT. I need to set the site to require SSL and I need to redirect HTTP to HTTPS. Could someone give some assistance?

Thanks,

Richard

Internet Information Services
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sam Wu-MSFT 7,046 Reputation points Microsoft Vendor
    2022-03-22T02:38:26.357+00:00

    @richardwolford-7948

    I need to set the site to require SSL

    You can retrieve the binding information of your site using the Get-WebBinding cmdlet and set the SSL Certificate using the AddSslCertificate function:

    $siteName = 'mywebsite'  
    $dnsName = 'www.mywebsite.ru'  
      
    # create the ssl certificate  
    $newCert = New-SelfSignedCertificate -DnsName $dnsName -CertStoreLocation cert:\LocalMachine\My  
      
    # get the web binding of the site  
    $binding = Get-WebBinding -Name $siteName -Protocol "https"  
      
    # set the ssl certificate  
    $binding.AddSslCertificate($newCert.GetCertHashString(), "my")  
    

    I need to redirect HTTP to HTTPS

    You can refer to this link: PowerShell script that automatically adds a HTTP to HTTPS redirect rule to a web site's web.config..

    More information about this question you can refer to below links:

    How to configure SSL on IIS with PowerShell.
    How to configure SSL on IIS with PowerShell.
    Create a http to https URL redirect in IIS with Powershell.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments