Loading...

Upload files to Sharepoint/Teams using PowerShell

1 A+ A-


In this post I will show you how to upload files, folder on Sharepoint/Teams with PowerShell.


Context

- You have a Sharepoint site

- You want to upload content like log on a folder


Hoes does it works ?

We will use two methods to authenticate to Sharepoint:

- Through an Azure app

- Through a Sharepoint app


Special thanks to @veronicageek to help me understanding sharepoint part :-)


Using an Azure app

1. Create an Azure app

2. Add API permissions to Sharepoint

3. Create a certificate on a device

4. Upload the certifcate on the Azure app

5. Export the certificate

6. Install PowerShell.PnP module

7. Connect to the Sharepoint site

8. Use cmdlet to upload content


Using a Sharepoint app

1. Create the Sharepoint app

2. Trust the app

3. Add permissions

4. Install PowerShell.PnP module

5. Connect to the Sharepoint site

6. Use cmdlet to upload content


Our Sharepoint site

In our example, we have a Sharepoint site as below:

We want to upload log of a device in the folder Device logs.


To upload content in a folder you have to get its path on Sharepoint.

For that use the below code:


Install pnp module

To interact with your Sharepoint site we will use the module PowerShell.pnp

Type the below command to install it:


Using an Azure app

Create the Azure app

1. Go to Azure portal

2. Go to App registrations

3. Click on New registration

4. Type a name for your app

5. Let by default and click on Register

6. Go to Overview

7. Keep in mind Client id

8. Go to API permissions

9. Click on Add a permission

10. Select Sharepoint

11. Select Application permissions

12. Check Sites.FullControl.All

13. Click on Add permissions

14. Click on Grant admin consent


Generate the certificate

On a device, type the below command:

A CER file will be generated


Upload the certificate

1. Go to Azure portal

2. Go to your Azure app

3. Click on Certificates & secrets

4. Click on Upload certificate

5. Browse to the CER file

6. Click on Add

7. Keep in mind the Thumbprint

8. You can export certificate using the below command:



Using a Sharepoint app

Create Sharepoint app

1. Connect to your Sharepoint site

2. Add this to the address: /_layouts/15/AppRegNew.aspx

3. In my case the address will be the below one:

https://m365x190443.sharepoint.com/sites/SystandDeploy/_layouts/15/AppRegNew.aspx

4. In Client id click on Generate 

5. In Client Secret click on Generate

6. Type a title like Collect device logs

7. In App domain, type localhost

8. In Redirect URI, type localhost

9. Click on Create

10. The below confirmation appears:


Add permissions to the app

1. Connect to your Sharepoint site

2. Add this to the address: /_layouts/15/appinv.aspx

3. In my case the address will be the below one:

https://m365x190443.sharepoint.com/sites/SystandDeploy/_layouts/15/appinv.aspx

4. In App id, type the previous app ID

5. Click on Lookup

6. Infos of your app will be displayed

7. In Permissions add the below XML:

8. Click on Create

9. Click on Trust it


PowerShell and Sharepoint

Now we have prepared both connexion methods, let's see how to:

- Connect to our Sharepoint site

- List content from Sharepoint

- Get path of a specific folder

- Upload content to this folder


Connect through Azure app

To connect to our Sharepoint site using our Azure application we will need:

- The Sharepoint site URL

- The Azure app client ID

- The Azure app thumbprint


Then use the below command to connect:

By default, it will return nothing, meaning connexion is OK.


Connect through Sharepoint app

To connect to our Sharepoint site using our Azure application we will need:

- The Sharepoint site URL

- The Sharepoint app client ID

- The Sharepoint app client Secret


Then use the below command to connect:

By default, it will return nothing, meaning connexion is OK.


List content from List

The first step is to get List from Sharepoint.

In our example the list to use will be Documents, as below from the Sharepoint portal.

We will need the list title to be able to get content from Documents.

For that we will use the cmdlet: Get-PnPList

See below the result:

As you may noticed we will use the list Documents. 


List content from the list 

Now we have the name of the list, we will list its content.

For that we will use the following cmdlet: Get-PnPListItem

See below the command:

See below the result: 

The default result does not give enough informations.

We will use the below command to get more infos:

See below result:



Get the path of a folder

Now we want to get the path of the folder where to upload content.

In our example, the folder is called: Device logs

For that, type the below command:


Upload content to Sharepoint

Now we have the folder path we will use a file into it.

We will upload a ZIP file.

The cmdlet to use is the following: Add-PnPFile

To this one we need to add below parameters:

- Path: Path of the file to upload

- Folder: Path of the Sharepoint folder where to upload file


See below the full command:


Teams 5567391499376913677

Enregistrer un commentaire

1 commentaire

Frafau a dit…
Ce commentaire a été supprimé par l'auteur.
Accueil item

Award

Learn KQL in one month

Sponsors

You want to support me ?

Mes articles en français

Books in French


Stats