Home > Blog > How to Run a .PS1 File in SharePoint Online Management Shell
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:\ScriptPath\Script.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!

Additional How-To Articles

 

How to Install Microsoft Teams

How to Install Microsoft Teams

One of the main functions of SharePoint is document management. While SharePoint as a platform is versatile and can wear numerous hats, it shines as a file storage and collaboration tool. Many users are comfortable structuring document shares using...

How to Create a SharePoint Site

How to Create a SharePoint Site

Before we get into how to create a SharePoint site, here is a brief history lesson regarding SharePoint and its terminology. For years, when discussing SharePoint on-premises architecture, the community differentiated between site collections and...

How to Create a Folder in SharePoint

How to Create a Folder in SharePoint

One of the main functions of SharePoint is document management. While SharePoint as a platform is versatile and can wear numerous hats, it shines as a file storage and collaboration tool. Many users are comfortable structuring document shares using...

0 Comments

[wpforms id="1422"]
<div class="wpforms-container wpforms-container-full" id="wpforms-1422"><form id="wpforms-form-1422" class="wpforms-validate wpforms-form" data-formid="1422" method="post" enctype="multipart/form-data" action="/blog/how-to-run-a-ps1-file" data-token="767c06098274272e3574480cbb565d38"><noscript class="wpforms-error-noscript">Please enable JavaScript in your browser to complete this form.</noscript><div class="wpforms-field-container"><div id="wpforms-1422-field_0-container" class="wpforms-field wpforms-field-name" data-field-id="0"><label class="wpforms-field-label" for="wpforms-1422-field_0">Full Name <span class="wpforms-required-label">*</span></label><input type="text" id="wpforms-1422-field_0" class="wpforms-field-large wpforms-field-required" name="wpforms[fields][0]" required></div><div id="wpforms-1422-field_1-container" class="wpforms-field wpforms-field-email" data-field-id="1"><label class="wpforms-field-label" for="wpforms-1422-field_1">Email <span class="wpforms-required-label">*</span></label><input type="email" id="wpforms-1422-field_1" class="wpforms-field-large wpforms-field-required" name="wpforms[fields][1]" required></div><div id="wpforms-1422-field_3-container" class="wpforms-field wpforms-field-phone" data-field-id="3"><label class="wpforms-field-label" for="wpforms-1422-field_3">Phone <span class="wpforms-required-label">*</span></label><input type="tel" id="wpforms-1422-field_3" class="wpforms-field-large wpforms-field-required wpforms-masked-input" data-inputmask="&#039;mask&#039;: &#039;(999) 999-9999&#039;" data-rule-us-phone-field="true" name="wpforms[fields][3]" required></div><div id="wpforms-1422-field_2-container" class="wpforms-field wpforms-field-textarea" data-field-id="2"><label class="wpforms-field-label" for="wpforms-1422-field_2">How can we help you? <span class="wpforms-required-label">*</span></label><textarea id="wpforms-1422-field_2" class="wpforms-field-small wpforms-field-required" name="wpforms[fields][2]" required></textarea></div><div id="wpforms-1422-field_15-container" class="wpforms-field wpforms-field-html" data-field-id="15"><div id="wpforms-1422-field_15"><p style="padding-top: 10px; padding-bottom: 10px; font-family: 'Open Sans', sans-serif; font-size: 10px; color: #333333;">By clicking the button below, you are agreeing to our <span style="text-decoration: underline;"><span style="color: #000000;"><a style="color: #000000; text-decoration: underline;" href="https://www.incworx.com/privacy-policy">Privacy Policy</a></span></span>.</p></div></div><div id="wpforms-1422-field_7-container" class="wpforms-field wpforms-field-hidden" data-field-id="7"><input type="hidden" id="wpforms-1422-field_7" name="wpforms[fields][7]" value="How to Run a .PS1 File in SharePoint Online Management Shell"></div><div id="wpforms-1422-field_9-container" class="wpforms-field wpforms-field-hidden" data-field-id="9"><input type="hidden" id="wpforms-1422-field_9" name="wpforms[fields][9]" value="103.131.71.190"></div><div id="wpforms-1422-field_10-container" class="wpforms-field wpforms-field-hidden" data-field-id="10"><input type="hidden" id="wpforms-1422-field_10" name="wpforms[fields][10]"></div></div><div class="wpforms-field wpforms-field-hp"><label for="wpforms-1422-field-hp" class="wpforms-field-label">Comment</label><input type="text" name="wpforms[hp]" id="wpforms-1422-field-hp" class="wpforms-field-medium"></div><div class="wpforms-submit-container" ><input type="hidden" name="wpforms[id]" value="1422"><input type="hidden" name="wpforms[author]" value="2"><input type="hidden" name="wpforms[post_id]" value="8444"><button type="submit" name="wpforms[submit]" class="wpforms-submit " id="wpforms-submit-1422" value="wpforms-submit" aria-live="assertive" data-alt-text="Sending..." data-submit-text="Submit">Submit</button></div></form></div> <!-- .wpforms-container -->