Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Create a Self-Signed Certificate Using New-SelfSignedCertificate in PowerShell

In the world of Windows systems administration, secure communication is paramount. One way to ensure secure communication is by using certificates. A self-signed certificate is an SSL certificate that is signed by the person creating it rather than a trusted certificate authority. This can be useful for testing, development, and internal network applications where you control the environment.


The New-SelfSignedCertificate cmdlet in PowerShell allows you to create self-signed certificates easily. This cmdlet is particularly useful for creating certificates for development and testing purposes, where the overhead of obtaining a certificate from a trusted Certificate Authority (CA) is unnecessary.


In this article, you will learn how to create a self-signed certificate using the New-SelfSignedCertificate cmdlet in PowerShell. We will cover practical examples to help you understand how to use this cmdlet effectively.


Examples:


1. Creating a Basic Self-Signed Certificate:
To create a basic self-signed certificate, open PowerShell with administrative privileges and run the following command:


   New-SelfSignedCertificate -DnsName "www.example.com" -CertStoreLocation "cert:\LocalMachine\My"

This command creates a self-signed certificate for the domain www.example.com and stores it in the "My" store of the local machine.


2. Creating a Self-Signed Certificate with a Specific Key Length:
You can specify the key length of the certificate by using the KeyLength parameter. For example:


   New-SelfSignedCertificate -DnsName "www.example.com" -CertStoreLocation "cert:\LocalMachine\My" -KeyLength 2048

This command creates a certificate with a 2048-bit key length.


3. Creating a Self-Signed Certificate with a Custom Expiration Date:
By default, the certificate is valid for one year. You can change the validity period using the NotAfter parameter:


   New-SelfSignedCertificate -DnsName "www.example.com" -CertStoreLocation "cert:\LocalMachine\My" -NotAfter (Get-Date).AddYears(2)

This command creates a certificate that is valid for two years.


4. Creating a Self-Signed Certificate for Multiple DNS Names:
You can create a certificate that is valid for multiple DNS names by using the -DnsName parameter with multiple values:


   New-SelfSignedCertificate -DnsName "www.example.com", "example.com" -CertStoreLocation "cert:\LocalMachine\My"

This command creates a certificate that is valid for both www.example.com and example.com.


5. Exporting the Self-Signed Certificate:
After creating the certificate, you might want to export it to a file. You can use the Export-Certificate cmdlet for this purpose:


   $cert = New-SelfSignedCertificate -DnsName "www.example.com" -CertStoreLocation "cert:\LocalMachine\My"
Export-Certificate -Cert $cert -FilePath "C:\Path\To\Export\example.cer"

This command exports the created certificate to a file named example.cer.


To share Download PDF