title: Saving credentials for reuse by powershell and error ConvertTo-SecureString : Key not valid for use in specified state
body: <p>I was doing something like described in this post to save credentials in a secured file so our automated process can use that to run remote PS scripts via Invoke-command:
  <a href="http://blogs.technet.com/b/robcost/archive/2008/05/01/powershell-tip-storing-and-using-password-credentials.aspx" rel="noreferrer">http://blogs.technet.com/b/robcost/archive/2008/05/01/powershell-tip-storing-and-using-password-credentials.aspx</a></p>
  
  <p>This works great when I run this under my account - password is read from encrypted file, passed to Invoke-command and everything is fine.</p>
  
  <p>Today, when my script was ready for its prime time, I tried to run it under windows account that will be used by automated process and got this error below while my script was trying to read secured password from a file:</p>
  
  <pre><code>ConvertTo-SecureString : Key not valid for use in specified state.
  At \\remoted\script.ps1:210 char:87
  + $password = get-content $PathToFolderWithCredentials\pass.txt | convertto-sec
  urestring &lt;&lt;&lt;&lt;
      + CategoryInfo          : InvalidArgument: (:) [ConvertTo-SecureString], C
     ryptographicException
      + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_Cryptographic
     Error,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
  </code></pre>
  
  <p>Asked my workmate to run under his account and he got the same error.</p>
  
  <p>This is the code I am using to save credentials:</p>
  
  <pre><code>$PathToFolderWithCredentials = "\\path\removed"
  
  write-host "Enter login as domain\login:"
  read-host | out-file $PathToFolderWithCredentials\login.txt
  
  write-host "Enter password:"
  read-host -assecurestring | convertfrom-securestring | out-file $PathToFolderWithCredentials\pass.txt
  
  write-host "*** Credentials have been saved to $pathtofolder ***"
  </code></pre>
  
  <p>This is the code in the script to run by automated process to read them to use in Invoke-command:</p>
  
  <pre><code>$login= get-content $PathToFolderWithCredentials\login.txt
  $password = get-content $PathToFolderWithCredentials\pass.txt | convertto-securestring
  $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $login,$password
  </code></pre>
  
  <p>Error happens on line $password = get-content $PathToFolderWithCredentials\pass.txt | convertto-securestring</p>
  
  <p>Any ideas?</p>