Trying to remove PW from script and prompt for user input for it

Hi all,
I have this script I have been using:

Import-PfxCertificate -FilePath C:\Temp\certs\xxxxx.pfx -Password (ConvertTo-SecureString -String 'xxxxx' -AsPlainText -Force) -CertStoreLocation Cert:\LocalMachine\My | out-file -Append -FilePath c:\temp\certs.txt
Import-PfxCertificate -FilePath C:\Temp\certs\xxxxx.pfx -Password (ConvertTo-SecureString -String 'xxxxx' -AsPlainText -Force) -CertStoreLocation Cert:\LocalMachine\My | out-file -Append -FilePath c:\temp\certs.txt

Is there anyway to remove the hard coded PW from it and have it prompt for user input?

Thanks!

Did you try to search for it?

https://www.google.com/search?q=powershell+prompt+for+user+input

I did, and I’ve been trying this, but not quite there:

Import-PfxCertificate -FilePath C:\Temp\certs\xxxxx.pfx -Password (Read-Host -Prompt "Please enter the password") -CertStoreLocation Cert:\LocalMachine\My | out-file -Append -FilePath c:\temp\certs.txt

The error I get is this:

Import-PfxCertificate : Cannot bind parameter 'Password'. Cannot convert the "12345" value of type "System.String" to type "System.Security.SecureString"

I was trying various ways of using ConvertTo-SecureString -String, but I dont have the correct syntax, or its just wrong.

You should always read the help for the cmdlets you’re about to use completely including the examples to löearn how to use them. There is even an example for particular purpose

$Password = Read-Host -Prompt "Please enter the password" -AsSecureString
Import-PfxCertificate -FilePath C:\Temp\certs\xxxxx.pfx -Password $Password -CertStoreLocation Cert:\LocalMachine\My | 
    out-file -Append -FilePath c:\temp\certs.txt

… and please … your code will be way easier to read and to understand when you use line breaks after each pipe symbol. You should read the following topic as well:

Thanks alot Olaf! I had written this before I looked at yours just to test, and finally got it working

$password  = Read-Host -Prompt "Please enter the password" -AsSecureString
Import-PfxCertificate -FilePath C:\scripts\nvidia.pfx -Password ($password) -CertStoreLocation Cert:\LocalMachine\My | 
out-file -Append -FilePath c:\scripts\certs.txt

Appreciate it. Each day I learn a little bit more.