Connecting to PnP PowerShell in SharePoint Online Using AppID and AppSecret

Yunus Emre Araç
4 min readApr 10, 2023

Reference: https://www.yunusemrearac.com/2023/04/02/sharepoint-onlineda-pnp-powershelle-appid-ve-appsecret-kullanarak-baglanti-saglamak/

Hello friends, today I will show you a method on how to connect a PnP PowerShell script that you will write on the sharepoint online side without using a username and password.

For example, you have a script written in a pnp powershell and you want to run it as a scheledur. But if you do not want to write the user email address and password directly into the script due to information security, you can connect without any problems by following the steps below.

Now we log in to our sharepoint online site, which we will connect to first. To create a new app register, we add “/_layouts/15/AppRegNew.aspx” to the end of the root address of our site and go to the extension.

On the next screen, we create both our “Client Id” and “Client Secret” parameters by pressing the “Generate” button and write them down. In the “Title” section, you can write the title you will give for this app registration. If you write in this section according to your intended use, it will be useful for you in the future. I named it “PnP PowerShell Script” because I want to use it for PnP Powershell script. After typing “localhost” for the “App Domain” section and typing https://localhost" in the “Redirect URL” field, we complete the creation process with the “Create” button.

If your app registration process has been completed without any problems, you will be directed to a screen like the one above. If you have not saved the information in this section, you can retrieve it again and close the screen with the “OK” button.

In the next step, we need to define the authorization for the app register we have created. For this, we need to write “/_layouts/15/appinv.aspx” at the end of our site address and go to the app register authorization screen.

When we say “Lookup” by entering the “Client Id” information that we created on the previous screen into the “App Id” section on the incoming screen, the parts below find the app register we created and bring it. In the “Permission Request XML” section at the bottom, I write XML suitable for the level you want to authorize. I used the following xml because I want to give top level authority here.

<AppPermissionRequests AllowAppOnlyPolicy="true"><AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" /></AppPermissionRequests>

After completing this part, we give authorization with the “Create” button.

It will then display a confirmation screen on a screen to confirm the details of your authorization for this app and whether you trust this app. We complete the authorization process by clicking the “Trust It” button.

Then, you can see and delete the apps you have authorized by clicking “Site app permissions” under “Users and Permissions” in the site setting section or by going to “/_layouts/15/appprincipals.aspx?Scope=Web”.

As you can see, the app permission I created came here.

Now let’s complete our pnp powershell code with the app and app permission I created here.

$SiteURL = "https://yunusemrearac.sharepoint.com/"

Connect-PnPOnline -Url $SiteURL -ClientId "7803ee4c-d3fa-49d1-ba8c-xxxxxxxxxxxx" -ClientSecret "DsDVhxaCs+tv15pQT2DttBonzhCWzHguKYxxxxxxxxxx"

Get-PnPContext

When we write the “Client Id” and “Client Secret” information that we noted at the beginning to the powershell code above and run it, you will see that our powershell code is running at the level of authorization we have given.

As you can see above, I connected to my site with PnP Powershell script without any problems.

--

--

Yunus Emre Araç

Technology Product Manager of Corporate Applications at ING | Old Microsoft Student Partners Lead | İnönü Üniv. Bilg. Müh. | İAU Bilg. Müh. Tezli Yüksek Lisans