Requirement: Run PowerShell Script in SharePoint Online.
How to Run a PowerShell Script for SharePoint Online?
PowerShell is a powerful scripting language that can be used to automate SharePoint Online tasks! This blog post will show you how to execute PowerShell scripts for SharePoint Online and how you can use them to manage your SharePoint Online environment.
Unlike SharePoint On-premises PowerShell scripts that must be executed only from the server from the SharePoint farm, SharePoint Online PowerShell scripts can be run from the client-side (from your desktops!) when necessary modules are installed. This article is written for absolute beginners who need to run PowerShell scripts for SharePoint Online.
Step 1: Install the necessary PowerShell Modules for SharePoint Online
How to run a PowerShell script for SharePoint Online? Prior to executing PowerShell scripts for SharePoint Online, we must install the necessary module or components on our local machine. So, which module to install? Well, that depends on what cmdlets are used by your script.
- If your PowerShell script uses cmdlets for SharePoint Online management, then you must install the “SharePoint Online PowerShell Module”: How to Install SharePoint Online PowerShell Module?
- If you are using client side object model, then you must install SharePoint Online Client Components SDK: How to Install SharePoint Online Client Components SDK?
- How do I run a PnP PowerShell script for SharePoint Online? If your PowerShell script uses PnP PowerShell cmdlets, then you must have the PnP PowerShell module for SharePoint Online installed. How to Install PnP PowerShell Module for SharePoint Online?
Generally, it’s a good idea to have all of these three in your client machine to manage SharePoint Online using PowerShell.
Step 2: Use PowerShell ISE to run your PowerShell scripts for SharePoint Online
PowerShell ISE is a great tool to create, debug, and execute PowerShell Scripts. Although you can use Windows PowerShell console or SharePoint Online Management Shell, I recommend using PowerShell ISE. It gives you nice syntax highlighting, debugging with breakpoints, IntelliSense features, and much more. PowerShell ISE is pre-installed on operating systems Windows 7, Windows 2012, and later at “C:\WINDOWS\system32\WindowsPowerShel \v1.0”. On previous versions, like Windows Server 2008, You have to add it through Windows features if the PowerShell ISE app is not installed already.
How to Run PowerShell Scripts in SharePoint Online?
Set Execution Policy:
For security, PowerShell script execution is disabled by default on Windows 7 and Windows Server 2008 R2. Typing commands into the ISE and executing them will work. Still, once they are saved as a script, you will receive the following error when you attempt to execute it: ScriptName.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details
So, run “Set-ExecutionPolicy RemoteSigned” Once!
Alright, How to run SharePoint Online PowerShell scripts in PowerShell ISE?
- Go to Start >> Type “PowerShell ISE”.
- Right-click and Open PowerShell ISE with “Run as Administrator” if you have UAC enabled.
- Now, You can start writing your PowerShell script or copy-paste the script and then click on the “Run Script” button from the toolbar. (Shortcut key: F5)
Any output from your script will appear in the output window below the script editor. You can also use Visual Studio Code to execute PowerShell scripts.
How to run a ps1 file in SharePoint Online Management Shell?
SharePoint Online Management Shell is a Windows PowerShell module that lets you run commands to manage your SharePoint Online environment in Office 365. To run any PowerShell script file (.ps1) with SharePoint Online Management Shell, do the following:
- Start SharePoint Online Management Shell >> Navigate to the folder where your scripts are saved.
- Just type the name of the script file (Use the “tab” key to get the tab-completion of the file name or manually enter “.\<filename>.ps1”) and hit enter. E.g. We have a PowerShell script “Get-SPODeletedSites.ps1” saved in folder “C:\Scripts” that lists all deleted sites in the tenant.
You may have to enter your username and password and log in once (You need to have SharePoint Online Admin Permissions to connect to SPO Management Shell, BTW!). You can also run a PowerShell script by simply right-clicking on the .ps1 file and choosing “Run with PowerShell” from Windows File Explorer!
How do I run a ps1 script from the command line?
To execute a PowerShell script from the command line, use: PowerShell <Your-Script-path>
How do I connect to SharePoint Online from PowerShell? You have to establish the connection to SharePoint Online from PowerShell using the PowerShell command “Connect-SPOService” and then run any SharePoint Online PowerShell commands available. E.g.,
#Connect to SharePoint Online Admin center Connect-SPOService -URL "https://YourDomain-admin.sharepoint.com" -Credential (Get-Credential) #Get All Site collections Get-SPOSite #Get all SharePoint Online users from site Get-SPOUser -site "https://salaudeen.sharepoint.com/sites/retail"
You’ll get a prompt to enter your user name and password. Connecting to SharePoint Online from PowerShell is explained in another post: Connect to SharePoint Online PowerShell
In conclusion, running PowerShell scripts for SharePoint Online can be a powerful way for managing and automating tasks in your SharePoint environment. With the ability to create, retrieve, update and delete lists, libraries, sites and other SharePoint objects, you can streamline your workflow, automate repetitive tasks, and make your SharePoint environment more efficient. Remember that before running any scripts, it’s important to test them in a non-production environment, and to have the proper permissions and SharePoint Online module installed.
To connect with SharePoint Online with multifactor authentication enabled account, simply leave the Credential parameter from the “Connect-SPOService” cmdlet. E.g., Connect-SPOService -Url https://YourTenant-admin.sharepoint.com. If you are using PnP PowerShell SharePoint Module, use the “Interactive” switch, “Connect-PnPOnline -URL $SiteURL -Interactive”
More info: Connect to SharePoint Online PowerShell with MFA
Assuming you have the PnP PowerShell module for SharePoint Online installed, use the following command “Connect-PnPOnline” to connect with the SharePoint Online site. Once connected, you can perform operations on SharePoint List, List items, subsites, etc.
More info: How to Connect to SharePoint Online using PnP PowerShell?
You can automate your PowerShell scripts with the Windows task scheduler. Just open Task Scheduler from Start >> Administrative Tools, create a Basic Task, and follow the wizard. You can also schedule a PowerShell script with Azure Automation!
More info: How to Create a Scheduled Task for SharePoint Online PowerShell?
You can activate any SharePoint feature at the SharePoint site collection or site level using PowerShell. Features.Add method in CSOM or Enable-PnPFeature to enable a SharePoint feature.
More info: Enable SharePoint Online feature in PowerShell