Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Certificates

tags: PowerShell categories: System-Engineering

Table of Contents

List certificates

$Issuer = '*'
Get-ChildItem Cert:\LocalMachine -Recurse | Where-Object Issuer -match "CN=$Issuer"

List all expired certificates

$Issuer = '*'
$certs = Get-ChildItem Cert:\LocalMachine -Recurse | Where-Object Issuer -match "CN=$Issuer"
$ret = foreach($item in $certs){
   if($item.NotAfter.Date -le (Get-Date)){
      [PSCustomObject]@{
         Issuer     = $item.Issuer
         ValidFrom  = $item.NotBefore.Date
         ExpiresOn  = $item.NotAfter.Date
         KeyLength  = $item.PublicKey.Key.KeySize
         Thumbprint = $item.Thumbprint
      }
   }
}
$ret | Format-List

Import certificates

$Source = 'C:\temp\certstoimport'
$Target = 'Cert:\LocalMachine\Root'
foreach($item in (Get-ChildItem $Source -Filter '*.cer')){
   Import-Certificate -FilePath $item.FullName -CertStoreLocation $Target
}

Remove certificates

$Issuer = '*'
$certs  = Get-ChildItem Cert:\LocalMachine -Recurse | Where-Object Issuer -match "CN=$Issuer"
foreach($item in $certs){
   Remove-Item $item
}

See also

Certificate Provider on Microsoft Docs.

[ Top ] [ Blog ]