When
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.
Open the Extensions view, type PowerShell and select
the PowerShell extension.
Create a new .ps1 file.
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.
To work with a SharePoint Online site, connect to
the site using Interactive mode and your Client ID:
Connect-PnPOnline
-UrlURL TO
THE SITE-Interactive-ClientIdYOUR
ID
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-ModulePnP.PowerShell.
In the
next
article, we will see how you can avoid logging in to
SharePoint each time you are running a PowerShell script.