SOLVED

Store password in Windows credential manager and use it in Powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-36192%22%20slang%3D%22en-US%22%3EStore%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-36192%22%20slang%3D%22en-US%22%3E%3CP%3EOn%20the%20%23ESPC16%20in%20Vienna%20someone%20is%20showing%20a%20way%20to%20store%20credentials%20in%20the%20Windows%20credential%20manager%20and%20then%20use%20is%20in%20Powershell%20to%20connect%20to%20Exchange%20%2F%20SharePoint%20%2F%20Azure%E2%80%A6%20online.%3C%2FP%3E%3CP%3EIt%20was%20a%20very%20simple%20and%20I%20will%20use%20it%20for%20some%20scheduled%20tasks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20i%20don%E2%80%99t%20remember%20how%20he%20does%20it%20and%20I%20didn%E2%80%99t%20find%20such%20a%20good%20and%20simple%20way%20in%20the%20net.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnyone%20has%20it%20also%20seen%20or%20use%20it%20before%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-36192%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECredetialManager%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EESPC16%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38251%22%20slang%3D%22en-US%22%3ERe%3A%20Store%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38251%22%20slang%3D%22en-US%22%3EPnP%20PowerShell%20is%20the%20best%20solution%20ever%20%3A)%3C%2Fimg%3E%3CBR%20%2F%3EThank%20you%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EPnP%20PowerShell%20rocks!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-37866%22%20slang%3D%22en-US%22%3ERe%3A%20Store%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-37866%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20use%20a%20class%20which%20is%20available%20in%20the%20PnP%20Core%20module%20to%20use%20retrieve%20credentials%20from%20the%20Windows%20Credential%20Manager.%20That%20class%20you%20can%20find%20here%20%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-Sites-Core%2Fblob%2Fmaster%2FCore%2FOfficeDevPnP.Core%2FUtilities%2FCredentialManager.cs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-Sites-Core%2Fblob%2Fmaster%2FCore%2FOfficeDevPnP.Core%2FUtilities%2FCredentialManager.cs%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20would%20require%20you%20to%20either%20rewrite%20that%20to%20PowerShell%2C%20or%20use%20the%20PnP%20Core%20assembly%20in%20your%20project%20(or%20simply%20copy%20over%20this%20class%20to%20your%20own%20binary%20PowerShell%20module%20code).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlternatively%20you%20could%20install%20the%20PnP%20PowerShell%20cmdlets%20(install-module%20sharepointpnppowershellonline)%20and%20forget%20about%20all%20the%20other%20cmdlets%20and%20use%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGet-PnPStoredCredential%20-Name%20%22nameofthecredential%22%20-Type%20PSCredential%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-37836%22%20slang%3D%22en-US%22%3ERe%3A%20Store%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-37836%22%20slang%3D%22en-US%22%3E%3CP%3ESorry%2C%20i%20can't%26nbsp%3Banswer%20that%20question.%20Maybe%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%26nbsp%3Bcan%20provide%20some%20additional%20insight%20about%20how%20to%20use%20this%20approach%20with%20other%20modules.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-37780%22%20slang%3D%22en-US%22%3ERe%3A%20Store%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-37780%22%20slang%3D%22en-US%22%3EThat%E2%80%99s%20what%20I%E2%80%99ve%20seen%20at%20the%20ESPC16%20%E2%80%93%20but%20this%20only%20works%20with%20PnP%20%3F!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-37467%22%20slang%3D%22en-US%22%3ERE%3A%20Store%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-37467%22%20slang%3D%22en-US%22%3EThis%20is%20what%20I%20have%20used%20in%20the%20past%3A%20%3CA%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FPowerShell-Credentials-d44c3cde%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FPowerShell-Credentials-d44c3cde%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-36566%22%20slang%3D%22en-US%22%3ERe%3A%20Store%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-36566%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2FManipulate-credentials-in-58e0f761%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EPasswordVault%3C%2FA%3E%20module%20for%20this%2C%20and%20it%20works%20pretty%20great.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wrote%20a%20tiny%2C%20crude%20function%20that%20would%20allow%20me%20to%20export%20a%20saved%20credential%20to%20a%20%24CurrentCredential%20variable%20to%20use%20for%20automation%20and%20such%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Efunction%20Save-CredentialToVariable%20(%24CredentialLookupString)%0A%23%23%23%23%23%23%23%23%20This%20function%20saves%20a%20credential%20from%20vault%20to%20a%20variable%0A%20%7B%0A%20%20%24global%3ACurrentCredential%20%3D%20Get-VaultCredential%20%7C%20Where-Object%20%7B%24_.Resource%20-match%20%24CredentialLookupString%7D%20%7C%20ConvertTo-Credential%0A%20%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-36270%22%20slang%3D%22en-US%22%3ERe%3A%20Store%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-36270%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20should%20help%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fwiki%2FHow-to-use-the-Windows-Credential-Manager-to-ease-authentication-with-PnP-PowerShell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fwiki%2FHow-to-use-the-Windows-Credential-Manager-to-ease-authentication-with-PnP-PowerShell%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20was%20written%20by%20the%20very%20helpful%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-36208%22%20slang%3D%22en-US%22%3ERe%3A%20Store%20password%20in%20Windows%20credential%20manager%20and%20use%20it%20in%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-36208%22%20slang%3D%22en-US%22%3E%3CP%3EIt's%20not%20exactly%20the%20same%20you%20asked%20for%20but%20it%20does%20the%20job%20so%20might%20be%20helpful%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fpractical365.com%2Fblog%2Fsaving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fpractical365.com%2Fblog%2Fsaving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks%2F%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

On the #ESPC16 in Vienna someone is showing a way to store credentials in the Windows credential manager and then use is in Powershell to connect to Exchange / SharePoint / Azure… online.

It was a very simple and I will use it for some scheduled tasks.

 

But i don’t remember how he does it and I didn’t find such a good and simple way in the net.

 

Anyone has it also seen or use it before?

8 Replies

I am using the PasswordVault module for this, and it works pretty great.

 

I wrote a tiny, crude function that would allow me to export a saved credential to a $CurrentCredential variable to use for automation and such:

 

function Save-CredentialToVariable ($CredentialLookupString)
######## This function saves a credential from vault to a variable
	{
		$global:CurrentCredential = Get-VaultCredential | Where-Object {$_.Resource -match $CredentialLookupString} | ConvertTo-Credential
	}
That’s what I’ve seen at the ESPC16 – but this only works with PnP ?!

Sorry, i can't answer that question. Maybe @Erwin van Hunen can provide some additional insight about how to use this approach with other modules.

best response confirmed by Heiko Watz (Contributor)
Solution

We use a class which is available in the PnP Core module to use retrieve credentials from the Windows Credential Manager. That class you can find here : 

 

https://github.com/SharePoint/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/Utilities/Credential...

 

This would require you to either rewrite that to PowerShell, or use the PnP Core assembly in your project (or simply copy over this class to your own binary PowerShell module code).

 

Alternatively you could install the PnP PowerShell cmdlets (install-module sharepointpnppowershellonline) and forget about all the other cmdlets and use:

 

Get-PnPStoredCredential -Name "nameofthecredential" -Type PSCredential

 

PnP PowerShell is the best solution ever :)
Thank you @Erwin van Hunen

PnP PowerShell rocks!