├── Functions-PSStoredCredentials.ps1 ├── LICENSE └── README.md /Functions-PSStoredCredentials.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Functions-PSStoredCredentials - PowerShell functions to manage stored credentials for re-use 4 | 5 | .DESCRIPTION 6 | This script adds two functions that can be used to manage stored credentials 7 | on your admin workstation. 8 | 9 | .EXAMPLE 10 | . .\Functions-PSStoredCredentials.ps1 11 | 12 | .LINK 13 | https://practical365.com/blog/saving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks 14 | 15 | .NOTES 16 | Written by: Paul Cunningham 17 | 18 | Find me on: 19 | 20 | * My Blog: https://paulcunningham.me 21 | * Twitter: https://twitter.com/paulcunningham 22 | * LinkedIn: https://au.linkedin.com/in/cunninghamp/ 23 | * Github: https://github.com/cunninghamp 24 | #> 25 | 26 | 27 | Function New-StoredCredential { 28 | 29 | <# 30 | .SYNOPSIS 31 | New-StoredCredential - Create a new stored credential 32 | 33 | .DESCRIPTION 34 | This function will save a new stored credential to a .cred file. 35 | 36 | .EXAMPLE 37 | New-StoredCredential 38 | 39 | .LINK 40 | https://practical365.com/saving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks 41 | 42 | .NOTES 43 | Written by: Paul Cunningham 44 | 45 | Find me on: 46 | 47 | * My Blog: http://paulcunningham.me 48 | * Twitter: https://twitter.com/paulcunningham 49 | * LinkedIn: http://au.linkedin.com/in/cunninghamp/ 50 | * Github: https://github.com/cunninghamp 51 | 52 | For more Office 365 tips, tricks and news 53 | check out Practical 365. 54 | 55 | * Website: https://practical365.com 56 | * Twitter: https://twitter.com/practical365 57 | #> 58 | 59 | if (!(Test-Path Variable:\KeyPath)) { 60 | Write-Warning "The `$KeyPath variable has not been set. Consider adding `$KeyPath to your PowerShell profile to avoid this prompt." 61 | $path = Read-Host -Prompt "Enter a path for stored credentials" 62 | Set-Variable -Name KeyPath -Scope Global -Value $path 63 | 64 | if (!(Test-Path $KeyPath)) { 65 | 66 | try { 67 | New-Item -ItemType Directory -Path $KeyPath -ErrorAction STOP | Out-Null 68 | } 69 | catch { 70 | throw $_.Exception.Message 71 | } 72 | } 73 | } 74 | 75 | $Credential = Get-Credential -Message "Enter a user name and password" 76 | 77 | $Credential.Password | ConvertFrom-SecureString | Out-File "$($KeyPath)\$($Credential.Username).cred" -Force 78 | 79 | # Return a PSCredential object (with no password) so the caller knows what credential username was entered for future recalls 80 | New-Object -TypeName System.Management.Automation.PSCredential($Credential.Username,(new-object System.Security.SecureString)) 81 | 82 | } 83 | 84 | 85 | 86 | Function Get-StoredCredential { 87 | 88 | <# 89 | .SYNOPSIS 90 | Get-StoredCredential - Retrieve or list stored credentials 91 | 92 | .DESCRIPTION 93 | This function can be used to list available credentials on 94 | the computer, or to retrieve a credential for use in a script 95 | or command. 96 | 97 | .PARAMETER UserName 98 | Get the stored credential for the username 99 | 100 | .PARAMETER List 101 | List the stored credentials on the computer 102 | 103 | .EXAMPLE 104 | Get-StoredCredential -List 105 | 106 | .EXAMPLE 107 | $credential = Get-StoredCredential -UserName admin@tenant.onmicrosoft.com 108 | 109 | .EXAMPLE 110 | Get-StoredCredential -List 111 | 112 | .LINK 113 | https://practical365.com/saving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks 114 | 115 | .NOTES 116 | Written by: Paul Cunningham 117 | 118 | Find me on: 119 | 120 | * My Blog: http://paulcunningham.me 121 | * Twitter: https://twitter.com/paulcunningham 122 | * LinkedIn: http://au.linkedin.com/in/cunninghamp/ 123 | * Github: https://github.com/cunninghamp 124 | 125 | For more Office 365 tips, tricks and news 126 | check out Practical 365. 127 | 128 | * Website: https://practical365.com 129 | * Twitter: https://twitter.com/practical365 130 | #> 131 | 132 | param( 133 | [Parameter(Mandatory=$false, ParameterSetName="Get")] 134 | [string]$UserName, 135 | [Parameter(Mandatory=$false, ParameterSetName="List")] 136 | [switch]$List 137 | ) 138 | 139 | if (!(Test-Path Variable:\KeyPath)) { 140 | Write-Warning "The `$KeyPath variable has not been set. Consider adding `$KeyPath to your PowerShell profile to avoid this prompt." 141 | $path = Read-Host -Prompt "Enter a path for stored credentials" 142 | Set-Variable -Name KeyPath -Scope Global -Value $path 143 | } 144 | 145 | 146 | if ($List) { 147 | 148 | try { 149 | $CredentialList = @(Get-ChildItem -Path $keypath -Filter *.cred -ErrorAction STOP) 150 | 151 | foreach ($Cred in $CredentialList) { 152 | Write-Host "Username: $($Cred.BaseName)" 153 | } 154 | } 155 | catch { 156 | Write-Warning $_.Exception.Message 157 | } 158 | 159 | } 160 | 161 | if ($UserName) { 162 | if (Test-Path "$($KeyPath)\$($Username).cred") { 163 | 164 | $PwdSecureString = Get-Content "$($KeyPath)\$($Username).cred" | ConvertTo-SecureString 165 | 166 | $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $PwdSecureString 167 | } 168 | else { 169 | throw "Unable to locate a credential for $($Username)" 170 | } 171 | 172 | return $Credential 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Paul Cunningham 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PowerShell-Stored-Credentials 2 | PowerShell functions to manage stored credentials on your admin computers 3 | --------------------------------------------------------------------------------