Powershell : Convert from PFX to PEM


I noticed I was manually doing PFX conversion from PFX to PEM, and this was getting quite a regular task, so I thought, why no automated this with a script, so I have some PFX files in a folder and I need to convert when all to PEM files using OpenSSL for use elsewhere on other platforms.

Note : You need to install OpenSSL for this to work, if you are using Windows then you need to download a binary executable from the link here

Warning : This script will delete the PFX files once done, if you would like to keep them, please comment out the line before you run the script

# Specify the folder path containing the PFX files
$folderPath = "C:\SSL\BatchA"

# Get a list of all PFX files in the specified folder
$pfxFiles = Get-ChildItem -Path $folderPath -Filter "*.pfx"

# Loop through each PFX file
foreach ($pfxFile in $pfxFiles) {

# Extract the file name without extension
$fileNameWithoutExtension = $pfxFile.BaseName

# Run OpenSSL commands for the current PFX file
openssl pkcs12 -in $pfxFile.FullName -nocerts -out "$folderPath\$fileNameWithoutExtension.key.pem" -nodes -passin pass:pass

openssl pkcs12 -in $pfxFile.FullName -nokeys -out "$folderPath\$fileNameWithoutExtension.cert.pem" -passin pass:pass

# Output status for the current file
Write-Host "Processed: $($pfxFile.Name)"

# Delete the original PFX file
Remove-Item -Path $pfxFile.FullName
Write-Host "Deleted: $($pfxFile.Name)"

Previous Post Next Post

Ω†Ω…ΩˆΨ°Ψ¬ Ψ§Ω„Ψ§ΨͺΨ΅Ψ§Ω„