I am trying to have my password secured and stored in a file so that I don't need to enter each time when I run the script.
First step, I ran the following, entered the password which got stored into E:\cred.txt file. The txt file now contains an encrypted password.
(Get-Credential).Password | ConvertFrom-SecureString | Out-File "E:\cred.txt"
$File = "E:\cred.txt"
$User = "firstname.lastname@example.org"
#### I have two different user accounts, one for admin and other for operator,
#### however both user accounts use same password.
$adminuser = $User
$operator = $User -replace "@domain.com"
#### I would need to read $File to get only the password
$pass = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList (Get-Content $File | ConvertTo-SecureString)
#saving credentials Get-Credential | Export-CliXml -Path c:\credential.xml #importing credentials to a variable $Credential = Import-CliXml -Path c:\credential.xml
#you could then write it to a file or, i say its a better approach to a registry key $SecurePassword = ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force | ConvertFrom-SecureString #now you are taking it back as a secure string $RegistrySecureString = $SecurePassword | ConvertTo-SecureString #you can aslo see the password $UserName = "NULL" $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $RegistrySecureString $Password = $Credentials.GetNetworkCredential().Password #P@ssw0rd