How to Run a .PS1 File in SharePoint Online Management Shell

Repetitive tasks are a necessary evil. Many IT administrators rely on PowerShell scripts to automate their duties and monitor systems. Scripts can be run to complete operations in Office 365 and SharePoint Online. A script is a collection of PowerShell commands (also known as cmdlets) saved as a text file with a .ps1 extension for later use.

How does one execute a .ps1 file against their SharePoint Online tenant? That’s where SharePoint Online Management Shell comes into play.

SharePoint Online Management Shell is a Windows PowerShell module created to manage SharePoint Online.

 

Permissions for SharePoint Online PowerShell Management

To run SharePoint Online PowerShell commands, you must be assigned the SharePoint or global administrator role in Office 365. 

Installing SharePoint Online Management Shell

There are two ways to install SharePoint Online Management Shell:

For option #2, your PC must have PowerShell 5 or newer. After opening a new PowerShell window as an administrator, you can run the following command to install SharePoint Online Management Shell:

Install-Module -Name Microsoft.Online.SharePoint.PowerShell

If you don’t have administrator privileges on your PC, you can add the Scope parameter to install the module only for the current user (you):

Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser

 

Connecting to SharePoint Online with Powershell

Once SharePoint Online Management Shell is installed on your PC, you need to use it to connect to SharePoint Online. (Note that the commands below may already be integrated into your script.)

You can connect using one of the following commands, based on whether your tenant has multifactor authentication (MFA) enabled:

Multi-Factor Authentication Enabled

Connect-SPOService -Url https://yourtenant-admin.sharepoint.com

Without Multi-Factor Authentication Enabled

Connect-SPOService -Url https://yourtenant-admin.sharepoint.com -Credential [email protected]

 

You will need to replace “yourtenant” with the URL of your Office 365 tenant and “[email protected]” with your Office 365 login. A dialog box will appear prompting you to enter your credentials.

Credential Prompt With Multi-Factor Authentication Enabled

Credential Prompt With Multi-Factor Authentication Enabled

 

Credential Prompt Without Multi-Factor Authentication Enabled

Credential Prompt Without Multi-Factor Authentication Enabled

 

Once the sign-in process is complete, you’re connected to SharePoint Online and ready to run your script.

 

Running a .PS1 File in SharePoint Online Management Shell

Now that you’ve installed SharePoint Online Management Shell and connected to SharePoint Online, you can run your .ps1 file. To do so, type or paste the following into the PowerShell console:

& “C:ScriptPathScript.ps1”

You’ll need to replace the fake path above with your script’s actual file path, which you can copy from the address bar of Windows File Explorer if needed:

Windows Explorer

Next, hit Enter on your keyboard to run the script. That’s all there is to it! 

 

Troubleshooting Running a .PS1 File In PowerShell

If PowerShell won’t let you run any .ps1 files, you may need to update your device’s execution policy. Execution policies are a safeguard put in place to prevent malicious scripts from running on your PC.

You can update your execution policy settings by running the following command in PowerShell:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Replace “RemoteSigned” with your desired execution policy. The available options are:

  • Restricted – No scripts can run.
  • AllSigned – Only scripts signed by a trusted publisher can run.
  • RemoteSigned – Scripts downloaded from the internet must be signed by a trusted publisher to run.
  • Unrestricted – All scripts can run.

If you don’t feel comfortable changing the execution policy, you can also unblock a script to run it without changing the execution policy.

What Office 365 tasks do you automate using scripts? Comment below and let us know!

Related Articles to Help Grow Your Knowledge

What Are The Benefits Of Outsourcing IT Services?
What Are The Benefits Of Outsourcing IT Services?

Are you trying to cut costs this year? You might want to take a look at your IT team. About 60% of businesses are outsourcing to reduce their expenses. Outsourcing IT services won't just cut costs, though. It can also benefit your entire business operation! On the...

What Is ETL Used For?
What Is ETL Used For?

Although it is a common term in some IT circles, you may not be familiar with its significance. So, what is ETL? What is ETL used for? Businesses have had to deal with the explosive growth of data for decades. To remain usable, it’s critical that all of this data gets...

Why Is Data Warehousing Important?
Why Is Data Warehousing Important?

Wondering if data warehousing is right for your business? Why is data warehousing important? Read on below to discover everything you need to know about a data warehouse. What is a data warehouse? It's a powerful way for businesses to store data in a centralized area....

Join over 1,000 people who receive insights, guides and advice for Microsoft technologies every month.