sai kumar
sai kumar

Reputation: 53

encrypt username in PowerShell

Need to encrypt username and password in PowerShell I've encrypted password using the below code.

$credential = Get-Credential

$credential.Password | ConvertFrom-SecureString |
    Set-Content E:\powershell\encrypted_password1.txt

But when I tried to encrypt username with the same code

$credential.username | ConvertFrom-SecureString |
    Set-Content E:\powershell\encrypted_user1.txt

Encountered with below error:

ConvertFrom-SecureString : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

Is there any way to encrypt username?

Upvotes: 0

Views: 2422

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19694

Exporting:

$credential = Get-Credential

$credential.Password | ConvertFrom-SecureString | Set-Content E:\powershell\encrypted_password1.txt
$credential.Username | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Set-Content E:\powershell\encrypted_user1.txt

Importing:

$Username = Get-Content E:\powershell\encrypted_user1.txt | ConvertTo-SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Username)
$Username = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$Password = Get-Content E:\powershell\encrypted_password1.txt | ConvertTo-SecureString

$Credential = New-Object -TypeName PSCredential -ArgumentList $Username, $Password

Upvotes: 2

Related Questions