Reputation: 15571
I am creating a file that has password with secureString as below
[string][ValidateNotNullOrEmpty()]$secureStringPwd = "password123"
$secpasswd = ConvertTo-SecureString $secureStringPwd -AsPlainText -Force
$secureStringText = $secpasswd | ConvertFrom-SecureString
Set-Content "C:\folder\user.txt" $secureStringText
Now I am trying to retrieve the password the following way
$Password = Get-Content "C:\folder\user.txt" | ConvertFrom-SecureString
$creds = New-Object System.Management.Automation.PSCredential ("user", $Password)
$creds.Password
$creds.UserName
But I am getting an error as below:
New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
At line:1 char:10
+ $creds = New-Object System.Management.Automation.PSCredential ("user ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Upvotes: 0
Views: 5275
Reputation: 6860
You need to Convert the password from the text file back into a Secure String
$Password = Get-Content "C:\folder\user.txt" | ConvertTo-SecureString
Also you need to get the password into plain text by using the Network Credentials
$creds.GetNetworkCredential().Password
Here is a working example
[string][ValidateNotNullOrEmpty()]$secureStringPwd = "password123"
$secpasswd = ConvertTo-SecureString $secureStringPwd -AsPlainText -Force
$secureStringText = $secpasswd | ConvertFrom-SecureString
Set-Content "C:\folder\user.txt" $secureStringText
$Password = Get-Content "C:\folder\user.txt" | ConvertTo-SecureString
$creds = New-Object System.Management.Automation.PSCredential ("user", $Password)
$creds.GetNetworkCredential().Password
$creds.UserName
Upvotes: 2