Issue when using -AsSecureString cmdlet

This topic contains 1 reply, has 1 voice, and was last updated by  Ali Hassan 1 year ago.

  • Author
  • #73975

    Ali Hassan

    Hi Guys,

    I am facing a very odd issue, I have a script which prompt for the user password and checks if the password is complex or not depending on some criteria I have set. Below is the script block that i am using

     $username = read-host "Please enter your username"
     $password = read-host "Please enter the password you want to test" -AsSecureString 

    So i enter the password as –> (just to test)

    As per the criteria I get there is an upper case in the password.

    But when I remove the -asSecureString parameter, and enter the same password. it seems to pass the check properly.

    Does using -AsSecureString change anything while storing the password?

  • #73976

    Ali Hassan

    Found a way!

    $password = read-host -assecurestring
    $decodedpassword =[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)) 

    This seems to work alright...


You must be logged in to reply to this topic.