DEV Community

Cover image for PnP Tips - Credentials
Jaime López
Jaime López

Posted on • Updated on

PnP Tips - Credentials

This is the first post of several articles with tips for working with PnP PowerShell commands for Office 365. Today, I will show you how to save your time when connecting to the site collections you want to work with.

If you usually work with PowerShell and Office 365 you'll notice that it is tedious work when several site collections are involved in the tasks you have to do. You normally have to call the Connect-PnPOnline command passing the site collection URL and you have to do this for each of the site collections. A new popup window will appear asking you for the credentials you want to use to connect to the site.


# PnP command for working with a specific site collection
Connect-PnPOnline -Url https://<tenant name>.sharepoint.com/sites/<site collection url>
# It will ask you for the credentials

Enter fullscreen mode Exit fullscreen mode

So, let's imagine you have to collect the features for 3 of your site collections. You may think about creating a script and ask for credentials as the first step and, then these credentials will be used for connecting to the site collections. Well, you are wasting much time coding the script than running manually all these actions.

There is another solution, an easy solution let me say. PnP provides with cmdlets that work with your local Credential Manager application. If you don't know what is this think of a repo that makes relationships between URLs and credentials, avoiding writing the credentials, again and again, you want to access the site. In other words, PnP allows you to store your credentials for each of your site collections easier than you think and save your time when manual actions have to be done.

What is the way to use these commands? Let me show you by example:


Add-PnPStoredCredential -Name https://<tenant name>.sharepoint.com/sites/<site collection url> -Username <user name>
# It will ask you the password

# You can now run this command the number of times you need without asking for credentials
Connect-PnPOnline -Url https://<tenant name>.sharepoint.com/sites/<site collection url>

Enter fullscreen mode Exit fullscreen mode

It seems easy, right? Yes, it does. Let me go further and share with you this script, Add-StoredCredentials.ps1, that makes your life even better. I implemented it to automate my tenant tasks, so it stores the same credentials for all the site collections of the tenant even the admin site.


# Run once and credentials will be saved for all your site collections
Add-StoredCredentials.ps1 -TenantName <tenant name> -UserName <user name> -Password $(ConvertTo-SecureString <password> -AsPlainText -Force)

Enter fullscreen mode Exit fullscreen mode

Give it a try and let me know your thoughts.

Latest comments (0)