Powershell: Pass SecureString (or PSCredential) as script parameter

1.4k Views Asked by At

I have a script named script.ps1 that starts like this:

Param([Parameter(Mandatory)][SecureString] $Password)

# do some stuffs

If I run it like this no problem:

powershell.exe -File script.ps1

But if I try to explicitely pass the SecureString it does not work:

powershell.exe -File script.ps1 -Password (Read-Host -AsSecureString)

I suspect that you can only pass classic strings as script arguments but I'm unable to find a confirmation of this anywhere.

I know you can do this and that works:

& ./script.ps1 -Password (Read-Host -AsSecureString)

But you are not always in a powershell session to start with.

TL/DR: should I use plain strings for passwords when using script parameters?

p.s: if PSCredential works better for my case that's ok too

0

There are 0 best solutions below