First Time Use of PowerShell, SharePoint Online

A PowerShell with SharePoint tutorial

PowerShell iconWhen you start using PowerShell, some steps need to be performed before you can run a script for the first time. In the demo below, Peter Kalmström shows these steps for first SharePoint Online. Refer to the On-Premises article if you use SharePoint On-Premises.

SharePoint and PowerShell editions

Most videos in this Tips-series were recorded with the PnP module SharePointPnPPowerShellOnline, which build on PowerShell version 5, and the PowerShell scripts were created in the app PowerShell ISE. PowerShell V5 and PowerShell ISE still works well for SharePoint On-premises, but if you use SharePoint Online there are differences. The actual code is written in the same way in all versions.

SharePoint Online

In 2024, Microsoft introduced the more powerful, cross-platform version 7 of PowerShell and the module PnP.PowerShell for SharePoint Online. It is recommended to work with this module in the source-code editor Visual Studio Code (which is not the same as Visual Studio).

Once VS Code and its PowerShell extension has been installed and registered, the code that is shown in this demo series is generally written in the same way as before.

Installation

Below are the steps to install PowerShell version 7 and VS Code, to set up Visual Studio Code for PowerShell scripting and to connect to SharePoint Online.

Note that step 9 must be performed by a Global Administrator, as permissions are set on behalf of the organization.
  1. Install PowerShell in the recommended way: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.4. In Windows, it should be done via winget.
  2. Open PowerShell as an Administrator.
  3. Set the Execution policy: Set-ExecutionPolicy  -ExecutionPolicy RemoteSigned
  4. Install the module: Install-Module PnP.PowerShell
  5. Install Visual Studio Code: https://code.visualstudio.com
  6. Run VS Code as an administrator.
  7. Open the Extensions view, type PowerShell and select the PowerShell extension.
  8. Create a new .ps1 file.
  9. Register the application. Give it a name and enter your tenant name: Register-PnPEntraIDAppForInteractiveLogin  -ApplicationName "YOUR NAME"  -Tenant  TENANTNAME.onmicrosoft.com  -Interactive

    Log in and Consent to and Accept the permissions.

    Note the Client ID that you get in the VS Code Terminal. It is needed to connect to SharePoint. The ID can also be found at Entra ID >Identity >Applications >App Registrations.
  10. To work with a SharePoint Online site, connect to the site using Interactive mode and your Client ID: Connect-PnPOnline  -Url URL TO THE SITE -Interactive -ClientId YOUR ID
  11. Now you can develop scripts and run PowerShell 7 code in VS Code in the same way as in PowerShell ISE.
The process to install PowerShell and VS Code and set it up for SharePoint Online modification is also described in detail in the book PowerShell with SharePoint from Scratch.

Next time you use VS Code with PowerShell, you must import the PnP module you installed the first time: Import-Module PnP.PowerShell.
 


In the next article, we will see how you can avoid logging in to SharePoint each time you are running a PowerShell script.
back icon next icon
Products Buy Services Tips Books Contact About Us Tools

Security and integrity

Copyright  Kalmstrom Enterprises AB  All rights reserved