Store credentials in a variable

How to store your credentials in a variable.

# User profile directory
$Path = $env:USERPROFILE

# Check in file already exists, otherwise create it and store credentials
if (!(Test-Path "$Path\Documents\WindowsPowerShell\File1.txt"))
New-Item -path "$Path\Documents\WindowsPowerShell" -name File1.txt -type "file"
(Get-Credential).Password | ConvertFrom-SecureString | Out-File $Path\Documents\WindowsPowerShell\File1.txt"
$File1 = "$Path\Documents\WindowsPowerShell\File1.txt"
Write-Host "Successfully created new file and stored credentials." -ForegroundColor Green
$File1 = "$Path\Documents\WindowsPowerShell\File1.txt"
Write-Host "$File1 already exists" -ForegroundColor Green

# Store username
$User1 = "[email protected]"

# Create one final variable to store the username and password
$Cred1 = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User1, (Get-Content $File1 | ConvertTo-SecureString)

If you want the credentials to be available every time you open e.g. PowerShell ISE, you put the following line in: %userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1"