'Powershell Import-Certificate Access is denied

I'm running WSL Ubuntu on Windows 11 for local web development. However, as the site I'm currently building requires subdomains I used caddy server to deal with local https. However, as the intermediate cert only lasts about a week I'm installing certificates manually, so I'm trying to automate this certificate importing process.

Right now I created a simple PowerShell 7 Script to automatically import this certificate. But, I'm getting following error

Import-Certificate: \wsl.localhost\Ubuntu\home\k2k\laravel\k2k-website\cert-import.ps1:7 Line | 7 | Import-Certificate -FilePath $RootCertificate -CertStoreLocation $Roo … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Access is denied. (0x80070005 (E_ACCESSDENIED))

Import-Certificate: \wsl.localhost\Ubuntu\home\k2k\laravel\k2k-website\cert-import.ps1:8 Line | 8 | Import-Certificate -FilePath $IntermidiateCertificate -CertStoreLocat … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Access is denied. (0x80070005 (E_ACCESSDENIED))

The script that I currently write is here

$RootCertificate = "docker/caddy/authorities/root.crt"
$IntermidiateCertificate = "docker/caddy/authorities/intermediate.crt"

$RootCertStoreLocation = "Cert:\CurrentUser\Root"
$IntermidiateCertStoreLocation = "Cert:\CurrentUser\CA"

Import-Certificate -FilePath $RootCertificate -CertStoreLocation $RootCertStoreLocation
Import-Certificate -FilePath $IntermidiateCertificate -CertStoreLocation $IntermidiateCertStoreLocation

Read-Host -Prompt "Press Enter to exit"

Basically I'm just using the file browser to get into the folder then double click the script to execute.

ps. I already tried running PowerShell as administrator and then running the script. Same results...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source