input string was not in a correct format

Rising Flight 5,216 Reputation points
2023-12-27T05:30:06.7+00:00

Hi

i have a service account by name ******@mydomain.com and i have saved the password in a text file in the path(c:\windows\temp\pwd.txt)

i want to execute the below 3 lines in PowerShell. When i execute second line i am getting the below error.

ConvertTo-SecureString : Input string was not in a correct format.

$un = "******@mydomain.com"
$pw = Get-Content 'c:\windows\temp\pwd.txt' | ConvertTo-SecureString
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $un, $pw
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. Dikky Ryan Pratama 1,470 Reputation points
    2023-12-27T07:06:58.84+00:00

    Hi Rising Flight,
    It seems like you're encountering an issue with converting the content of 'c:\windows\temp\pwd.txt' to a secure string. The ConvertTo-SecureString cmdlet expects the input to be a secure string, and if the content of your text file is not in the correct format, it will throw an error.

    Make sure that the content of 'c:\windows\temp\pwd.txt' is a secure string. If you have saved the password using ConvertTo-SecureString previously, you should use ConvertTo-SecureString when reading the password from the file.

    Here's an example:

    
    $un = "******@mydomain.com"
    
    $pw = Get-Content 'c:\windows\temp\pwd.txt' | ConvertTo-SecureString
    
    $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $un, $pw
    
    

    If the content of 'c:\windows\temp\pwd.txt' is not a secure string, you should use ConvertTo-SecureString to convert the plain text password to a secure string before creating the PSCredential object.

    
    $un = "******@mydomain.com"
    
    $plainTextPassword = Get-Content 'c:\windows\temp\pwd.txt'
    
    $securePassword = ConvertTo-SecureString -String $plainTextPassword -AsPlainText -Force
    
    $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $un, $securePassword
    
    

    This way, you ensure that the content of the text file is correctly converted to a secure string before creating the PSCredential object.

    Regards.

    3 people found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rising Flight 5,216 Reputation points
    2023-12-27T10:18:23.12+00:00

    will this work. lets say password for service account is abc

    Read-Host "EnterPassword" -AsSecureString | ConvertFrom-SecureString | Out-File "c:\windows\temp\pwdfile.txt" 
    EnterPassword: abc  
    
    $un = "******@mydomain.com"  
    $pw = Get-Content 'c:\windows\temp\pwdfile.txt' | ConvertTo-SecureString  
    $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $un, $pw 
    
    
    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2023-12-27T16:27:58.43+00:00

    Assuming the password in the file was in plain-text format, you almost had it right the first time. You just needed to inform the ConvertTo-SecureString of that fact.

    $un = "******@mydomain.com"
    $pw = Get-Content c:\junk\pw.txt | ConvertTo-Securestring -AsPlainText -force
    $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $un, $pw
    
    0 comments No comments

  3. Rising Flight 5,216 Reputation points
    2023-12-28T10:29:39.16+00:00

    I am confused here, if i use the below powershell syntax password is not encrypted

    $un = "******@mydomain.com"
    $pw = Get-Content c:\windows\temp\pw.txt | ConvertTo-Securestring -AsPlainText -force
    $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $un, $pw
    
    

    the below works fine but i am not sure if line number 3 needs to be edited

    1.Read-Host "EnterPassword" -AsSecureString | ConvertFrom-SecureString | Out-File "c:\windows\temp\pw.txt"  
    EnterPassword: abc    
    
    2.$un = "******@mydomain.com"   
    3.$pw = Get-Content 'c:\windows\temp\pw.txt' | ConvertTo-SecureString   
    4.$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $un, $pw 
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.