PnP PowerShell: Connect-PnPOnline using ClientID and ClientSecret

Requirement: Connect to SharePoint Online using PnP PowerShell Connect-PnPOnline with AppID and AppSecret

I have a PnP PowerShell script scheduled in the Windows Task scheduler that runs every 5 minutes. I need the script to connect to SharePoint Online unattended. Although I can store user names and passwords in the script, I don’t want to do that as the passwords are regularly updated by our company policy.

How to Connect to SharePoint Online using PnP PowerShell AppID and AppSecret?

The App method of authenticating allows us to run scripts without prompting username and password. Here are the steps to create a new app in SharePoint Online:

Step 1: Register a SharePoint App Principal

Register a new app in the app registry. Say, you need to connect to SharePoint Online site “”, Navigate to the URL:, and register a new app principal. Here is how to generate client id and client secret in SharePoint Online:

  • Click on the “Generate” button for both the “Client Id” and “Client Secret” fields. 
  • Provide a name to the principal. I’ve entered “Task Scheduler Script”
  • For domain and redirect URL, use “localhost” and “https://localhost” as the redirect URL.
  • Copy Client Id and Client Secret fields and click on the “Create” button to register app principal. You should get a confirmation message “The app identifier has been successfully created.”
    connect-pnponline appid appsecret

Step 2: Assign Permission to the App Principal

Once the app principal is registered, the next step is to grant permission to the app principal on SharePoint Online. We can scope it to the tenant, site collection, or web levels. Let’s grant this app principal with “Full Access” rights on the site collection.

  • Navigate to
  • In the “App Id” field, enter the “Client Id” you copied in the previous step and click on the “Lookup” button. This loads “Title”, “App Domain” and “Redirect URL” values matching the entered App Id that we created and allows us to set the app’s permissions. In the “Permission Request XML:”, Enter the following and click on the “Create” button:
    <AppPermissionRequests AllowAppOnlyPolicy="true"><AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" /></AppPermissionRequests> 
    Similarly, to grant tenant-level permissions, use:
    <AppPermissionRequests AllowAppOnlyPolicy="true"><AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" /> </AppPermissionRequests>
    How to Connect to SharePoint Online using Connect-PnPOnline AppID and AppSecret
  • Click on the “Trust it” button to grant the app permission to access SharePoint site collection with full access rights.
    Connect to SharePoint Online using PnP PowerShell AppID and AppSecret

Here I’ve configured “Full Control” in this site collection. You can also use: Read-Only, Write, Full Control on Lists, Webs, Site collections, or even tenant. Refer .

Connect to SharePoint Online using Connect-PnPOnline ClientId and ClientSecret

Now, you can connect to SharePoint Online with AppId and AppSecret (technically from any application!)

#Site collection URL
$SiteURL = ""

#Connect to SharePoint Online with ClientId and ClientSecret
Connect-PnPOnline -Url $SiteURL -ClientId "3c85uc19-f1b9-41ba-8c16-c3281x09b82" -ClientSecret "1KLekxb775bhs/C3*aqqWE6Gs13u4="


In case, you get “Connect-PnPOnline : Token request failed.” error, check your AppId and AppSecret. Or it may be expired. By default, its expiration date is 1 year! You can register an AppID from the Azure AD management portal’s App registration section and set the duration to “Never Expire”. And then Grant access to that AppId from SharePoint.

If you get “Exception has been thrown by the target of an invocation.” that simply means either your Client ID or Client secret is wrong, or not granted permissions.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

15 thoughts on “PnP PowerShell: Connect-PnPOnline using ClientID and ClientSecret

  • June 19, 2021 at 10:36 AM

    Change it to ClientId and ClientSecret

  • May 27, 2021 at 1:48 PM

    other than using app password, can you use app id or secret id using CSOM?

  • May 11, 2021 at 8:15 PM


    When I try to connect I get following issue:

    Connect-PnPOnline : A parameter cannot be found that matches parameter name ‘AppId’.

  • February 10, 2021 at 1:37 PM

    Thank you Salaudeen, very useful!

  • January 4, 2021 at 10:03 PM

    How secure is this?

  • December 18, 2020 at 12:09 PM

    I am getting Token Request failed issue.

  • December 17, 2020 at 4:50 PM

    Hello after following above steps are you able to do Get-PnPList ?? I am still getting 401- Unauthorized.. Shouldn’t that is the whole purpose of doing above exercise.. can you pls. check at your end and confirm..


  • October 1, 2020 at 3:57 PM


    it was necessary for the second part to in order to not have an access denied with SPO.

  • September 25, 2020 at 5:24 PM

    Is it safe to store the client ID and client secret in the file as plain text? Just curious.

    • September 25, 2020 at 7:47 PM

      My question was poorly worded. What’s the next step to using the secret securely?

    • December 3, 2020 at 2:43 PM

      Either using a credential manager (PNP has one I think, just unsure whether it can store app-regs). Then there is the possibility using environment Variables.

  • September 24, 2020 at 4:13 PM

    as you have mentioned the Apps can be created using the azure portal. However, I am not able to authenticate using that app id and secret

  • June 11, 2020 at 8:12 AM

    Very useful ! works perfectly, thanks


Leave a Reply