Home » Wiki » How to Setup SSL Certificate on IIS Using PowerShell?

How to Setup SSL Certificate on IIS Using PowerShell?

by | SSL Installation Guides

Setup SSL Certificate on IIS Using PowerShell

Configure SSL Certificate on IIS with PowerShell

Configuring an SSL certificates on IIS using PowerShell can help secure your websites and applications. Setup SSL Certificate on IIS Using PowerShell can be accomplished by using the powerful command-line interface of PowerShell to automate IIS configuration and deployment. SSL (Secure Sockets Layer) encrypts communication between the server and clients to prevent eavesdropping and tampering. With a few PowerShell commands, you can create self-signed certificates, import trusted certificates, bind certificates to sites, and more.

This comprehensive guide will walk you through the entire process of setting up SSL on IIS using PowerShell on Windows Server.

Key Takeaways

  • Self-signed certificates are easy to create in PowerShell but should only be used for testing. Purchase trusted SSL certificates for production sites.
  • You can easily import existing SSL certificates into the local computer’s certificate store using the Import-PfxCertificate cmdlet.
  • The New-IISSiteBinding and Set-IISServerManager cmdlets allow binding certificates to sites and enable SSL.
  • SSL can be enabled at the server level or on individual sites, applications, and virtual directories.
  • Renewed certificates can be seamlessly updated by re-importing them and rebinding to sites.
  • PowerShell provides efficient automation for SSL certificate deployment on IIS.

Prerequisites Before Configuring an SSL Certificate on IIS using PowerShell

Before you can set up SSL certificates on IIS using PowerShell, you need:

  • Windows Server 2012 or later with the Web Server (IIS) server role installed.
  • Familiarity with PowerShell. Experience running commands in an administrative PowerShell prompt.
  • An SSL certificate from a trusted certificate authority like DigiCert, GlobalSign, etc. Or you can create a self-signed cert for testing.
  • The certificate file, which contains the certificate and private key, is in .pfx (PKCS#12) format.
  • Permission to modify IIS settings and bind certificates to websites and applications.

5 Easy Steps to Setup SSL Certificate on IIS Using PowerShell

Follow this simple steps to setup SSL certificate on IIS with PowerShell:

  • Import the SSL Certificate into the Local Computer Store
  • Create a Self-Signed Certificate for Testing
  • Bind the SSL Certificate to an IIS Site
  • Enable SSL for Websites in IIS
  • Manage Certificate Renewals

Step 1 – Import the SSL Certificate into the Local Computer Store

The first step is to import your SSL certificate’s .pfx file into the local machine’s certificate store. This makes it available to bind to IIS sites and applications.

Use the Import-PfxCertificate cmdlet specifying the file path and password:

Import-PfxCertificate -FilePath C:\certificates\my-certificate.pfx -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString -String "pwd123" -Force -AsPlainText)

Where:

  • FilePath: Path to the .pfx file containing the certificate and private key
  • CertStoreLocation: Certificate store to import into (usually Cert:\LocalMachine\My)
  • Password: Password to decrypt the .pfx file

This will install the certificate into the computer’s Personal store under Certificates (Local Computer).

You can validate it imported correctly using Get-ChildItem:

Get-ChildItem Cert:\LocalMachine\My

Now, your SSL certificate is installed and ready to bind to IIS sites and applications.

Step 2: Create a Self-Signed Certificate for Testing

For testing purposes, you can easily create a self-signed SSL certificate in PowerShell using New-SelfSignedCertificate.

Note: Clients and browsers will not trust self-signed certificates. Only use them for development/testing.

Here’s an example command to generate a self-signed cert valid for 365 days:

New-SelfSignedCertificate -DnsName "testcert" -CertStoreLocation "Cert:\LocalMachine\My" -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -CertValidFor 365

This creates a certificate with the subject name testcert in the My store for the local machine.

You can view it by running Get-ChildItem again:

PS C:\> Get-ChildItem Cert:\LocalMachine\My
Thumbprint                                    Subject
1919e42212248840f630203a258b57bcd4a42212      CN=testcert

The certificate will be valid for 365 days from creation. The subject name will show the DnsName specified.

Some key parameters for New-SelfSignedCertificate:

  • DnsName: The certificate subject or common name
  • KeyExportPolicy: Make the private key exportable if needed
  • Provider: Crypto provider to generate the cert, like Microsoft Enhanced RSA
  • CertStoreLocation: Certificate store location to save the cert
  • KeyLength: Certificate key size, like 2048 or higher
  • HashAlgorithm: Signing algorithm like SHA256
  • CertValidFor: Number of days the cert should be valid for

Now, your self-signed test certificate is created and ready to use. You can bind it to sites in IIS to test HTTPS workflows before getting a trusted commercial certificate.

Next, we will cover binding this certificate to a website in IIS.

Step 3: Bind the SSL Certificate to an IIS Site

Once you have imported the certificate into the local machine store, you can bind it to websites, applications, and virtual directories in IIS.

Let’s bind it to a site called “MySite” using the New-IISSiteBinding cmdlet:

New-IISSiteBinding -Name "MySite" -IP "*" -Port 443 -Protocol "https" -CertificateThumbPrint "E661583E8FABEF4C0BEF694CBC41C28FB81CD870"

Where:

  • Name: Site name in IIS
  • IP: IP address binding
  • Port: 443 for HTTPS
  • Protocol: https
  • CertificateThumbPrint: Thumbprint of the SSL certificate to bind

This will create an HTTPS binding for the site using the specified certificate.

You can also bind certificates to sites using Set-IISServerManager:

Set-IISServerManager -Name "MySite" -BindingInformation "*:443:www.example.com" -Protocol "https" -CertificateHash "E661583E8FABEF4C0BEF694CBC41C28FB81CD870"

The process is the same for binding certificates to applications and virtual directories.

Step 4: Enable SSL for Websites in IIS

Once your certificate is bound to the site, you need to enable SSL in IIS. This can be done at the server level or per site.

To enable it for all sites at the server level:

Set-IISCentralCertProvider -EnableCentralSSL

For individual sites:

Set-IISSite -Name "MySite" -BindingInformation "*:443:www.example.com" -Protocol "https" -IPAddress "*" -HostHeader "www.example.com" -EnableSSL

This will make the site only accessible over HTTPS.

You can also enable SSL when creating new sites in PowerShell using New-IISSite with the -EnableSSL parameter.

Step 5: Manage Certificate Renewals

SSL certificates need to be renewed periodically before they expire.

The renewal process involves getting a new certificate file from your CA and importing it to replace the expiring cert.

Follow these steps:

  • Request and download the renewed certificate file in .pfx format.
  • Import the new certificate into the local machine store using the steps in Step 1.
  • Rebind the new certificate to your IIS sites and applications using the steps in Step 3.
  • The new certificate will now be active, and the old expired certificate can be removed.

Common PowerShell Commands for IIS SSL Certificates

Here are some other useful PowerShell commands for working with SSL certificates in IIS:

  • Get-IISSiteBinding: Get bindings for an IIS site, including HTTPS bindings.
  • Get-IISServerCert: List installed certificates on the local IIS server.
  • Get-IISSSLBinding: Get SSL bindings for sites, apps, and virtual directories.
  • Remove-IISSiteBinding: Remove an SSL binding.
  • Remove-PfxCertificate: Remove an imported certificate.
  • New-IISServerManager: Create a new IIS site with HTTPS binding.
  • Set-IISCentralCertProvider: Enable or disable central SSL certificate management.

Troubleshooting Common SSL Certificate Issues

Here are some common issues when setting up SSL certificates in IIS with PowerShell:

  • Certificate import fails: The .pfx file password may be incorrect. Or you may not have permission to access the certificate store.
  • Cannot bind certificate: Make sure the certificate thumbprint matches the installed certificate. Or the binding information like port and hostname may be incorrect.
  • SSL is not working on-site. Double-check that SSL is enabled at both the server and site levels in IIS. The binding should show HTTPS enabled.
  • Certificate warnings: If users see certificate warnings, the root CA may not be trusted. Install the intermediate and root CA certificates.
  • Permission denied errors: Run PowerShell as Administrator to modify IIS settings and bind certificates.
  • Expired certificate: Follow the renewal steps to replace expired certificates seamlessly.

Final Thoughts

Configuring SSL certificates in IIS securely encrypts communication and data for websites and applications. PowerShell provides an efficient way to automate the deployment and management of SSL certificates.

Following the steps outlined in this guide, you can import trusted certificates, create and bind to sites, enable HTTPS, and seamlessly renew certificates.

PowerShell lets you manage SSL certificates across multiple IIS servers through reusable scripts. This enhances security and simplifies automation for applications and services using IIS.

The key is having the right SSL certificates from trusted CAs, importing them securely, and properly binding them to sites and apps in IIS. To ensure certificates never expire, automate routine tasks like renewals with PowerShell.

With the power of PowerShell, you can easily set up robust SSL implementations on Windows IIS to meet security and compliance requirements.

Frequently Asked Questions

Here are some common questions about setting up SSL certificates on IIS using PowerShell:

What are the benefits of using PowerShell for SSL certificates?

PowerShell makes it easy to automate SSL certificate deployment, binding, and management on IIS. Scripts rather than manual configuration allow you to manage multiple servers and sites.

Is it better to install certificates per site or globally?

Installing globally at the IIS server level allows sharing across sites. However, installing per site gives more granular control and separation. Choose based on your needs.

How do I create a CSR and purchase an SSL cert for IIS?

Use the New-SelfSignedCertificate cmdlet to generate a CSR. To purchase the certificate, submit it to a trusted CA like DigiCert. Then, import this commercial certificate into IIS.

Can I use one SSL certificate across multiple domains?

Yes, you need a multi-domain (SAN) certificate. This will allow you to bind the cert to multiple IIS sites with different domains.

How can I troubleshoot SSL certificate problems in IIS?

Check for errors during import, verify the binding, ensure SSL is enabled, validate intermediate and root CA certs, and test renewal before expiration.

Is there a way to automate SSL certificate renewal?

Yes, you can script the renewal process in PowerShell, including importing new certificates and rebinding. You can also use automation tools like DSC or Ansible playbooks.

What permissions do I need to manage SSL certificates in IIS?

You need admin access to the server for IIS configuration changes, and local administrator or user access to the certificate store is required.

Priya Mervana

Priya Mervana

Verified Badge Verified Web Security Experts

Priya Mervana is working at SSLInsights.com as a web security expert with over 10 years of experience writing about encryption, SSL certificates, and online privacy. She aims to make complex security topics easily understandable for everyday internet users.