A few months ago I wrote one of my favorite blog posts, Connecting to Office 365 with Multiple Accounts in PowerShell without Losing Your Damned Mind and I think it’s safe to say that no damned minds have been lost connecting to multiple Office 365 tenants since. A lesser man might have stopped there. He might have felt his mark on the world was made and he might just bask in the glory of all of that love and adoration. Not this guy. Today, I present you an even better way to connect to Office 365 with multiple accounts in PowerShell without losing your damned mind and without loading any additional modules!*
*(as long as you already have the PnP PowerShell module installed, that is)
Between that fateful day and today the delightful folks that work on the PnP PowerShell module have added three cmdlets that work with Stored Credentials, Add-PnPStoredCredential, Get-PnPStoredCredential, Get-PnPStoredCredential.
While the PnP cmdlets themselves have been able to pull credentials directly out of the Credential Store you still needed an external module to pull credentials out for use with other modules, namely the Azure AD and MSOnline modules and the SharePoint Online module. I previously met that need with the Credential Manager module. Let’s see how it looks.
Use the Get-PnPStoredCredential cmdlet to retrieve a stored credential by name. I’ve only had it work if I specify the –Type parameter with the PSCredential value.
$creds = Get-PnPStoredCredential -Name MODAdmin -Type PSCredential
You can output the $creds object to make sure it it grabbed the account you expected. Then you can pass that object to another cmdlet, like Connect-AzureAD and use it to authenticate.
Connect-AzureAD -Credential $creds
Here’s how it looks.
I haven’t loaded the Credential Manager module since the stored credential cmdlets showed up in the PnP Module. I already have the PnP module installed everywhere, so it seems unnecessary.
Happy Credential Storing.
tk
ShortURL: https://www.toddklindt.com/PoshMultipleAccountsPnP