Requirement: Run PowerShell Script in SharePoint Online.
How to Run a PowerShell Script for SharePoint Online?
Unlike SharePoint On-premises PowerShell scripts that must be executed only from the server from 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 necessary PowerShell Modules for SharePoint Online
Prior to executing PowerShell scripts for SharePoint Online, we must install the necessary module or components to 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 “SharePoint Online PowerShell Module”(preferred) How to Install SharePoint Online PowerShell Module? or Install SharePoint Online Management Shell.
- 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?
- If your PowerShell script uses PnP PowerShell cmdlets, then you must have PnP PowerShell module for SharePoint Online installed. How to Install PnP PowerShell Module for SharePoint Online?
Generally, its 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 would recommend using PowerShell ISE as it gives you nice syntax highlighting, debug 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\WindowsPowerShell\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:
PowerShell scripts are disabled by default on both Windows 7 and Windows Server 2008 R2 for security. Typing commands into the ISE and executing them will work, but 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.
How to run a ps1 file in SharePoint Online Management Shell?
To run any PowerShell script file (.ps1) with SharePoint Online Management Shell,
- Open 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.