PasswordFunctions.ps1

something exciting

Some information about the exciting thing

Table of contents generated with markdown-toc


Script

function Save-Password {
<# Example

.EXAMPLE
New-Item -Path C:\ -Name MyPasswords -ItemType Directory
. .\Functions\Save-Password.ps1
Save-Password -Label UserName
Save-Password -Label Password

#>
    param(
        [Parameter(Mandatory)]
        [string]$Label
    )

    $securePassword = Read-host -Prompt 'Input password' -AsSecureString | ConvertFrom-SecureString
    $securePassword | Out-File -FilePath "C:\MyPasswords\$Label.txt"
}

function Get-Password {
<#
.EXAMPLE
$user = Get-Password -Label UserName
$pass = Get-Password -Label password

.OUTPUT
$user | Format-List

.OUTPUT
Label           : UserName
EncryptedString : domain\administrator

.OUTPUT
$pass | Format-List
Label           : password
EncryptedString : SomeSecretPassword

.OUTPUT
$user.EncryptedString
domain\administrator

.OUTPUT
$pass.EncryptedString
SomeSecretPassword

#>
    param(
        [Parameter(Mandatory)]
        [string]$Label
    )

    $filePath = "C:\MyPasswords\$Label.txt"
    if (-not (Test-Path -Path $filePath)) {
        throw "The password with Label [$($Label)] was not found!"
    }

    $password = Get-Content -Path $filePath | ConvertTo-SecureString
    $decPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
    [pscustomobject]@{
        Label = $Label
        EncryptedString = $decPassword
    }
}

Back to Top


Download

Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top