How to automate a Windows PowerShell script to run against a Microsoft 365 domain
Published Date : 04 Nov 2014
Last Updated : 03 Oct 2024
Content Ref: TEC4194075
Operating System
(none)
Part No
(none)
Summary
Describes how to use Task Scheduler to automate the running of a Windows PowerShell script against a Microsoft 365 domain.
Symptoms
As an administrator of Microsoft® 365™ you may want to schedule a Windows® PowerShell® script to run regularly against your Microsoft 365 domain. This article details how to achieve this by creating a scheduled task to automatically log on to Microsoft 365 and run a PowerShell script. It assumes you have a working PowerShell script or PowerShell commands that you can already run manually with success.
Requirements
You will need the following:
A server with Windows PowerShell v3 installed. Windows Server® 2012 has v3 by default but Windows Server 2008 does not.
A local or domain administrator account for the server.
PowerShell script containing the commands you want to run and which you can manually run with success in the Microsoft 365 domain.
Microsoft 365 global administrator credentials.
Procedure
Encrypt the Microsoft 365 administrator password
As the logged on administrator, browse to My Documents (or Documents depending on your operating system).
In the folder, create a new folder called WindowsPowerShell.
In the WindowsPowerShell folder create a new blank file called creds.txt.
In the PowerShell ISE window, click View, Show Script Pane to ensure you can see the script pane (top, white section) and the Command Prompt Console (bottom, blue section).
Click View, 'Go to script pane'.
Copy and paste the following into the script pane, changing admin@schooldomain.onmicrosoft.com to the global administrator account in your Microsoft 365 domain:
For $M365username = enter your global administrator account.
For -ProxyAccessType, use the appropriate value to match how the server connects to the Internet. For example, use -ProxyAccessType IEConfig if Windows Internet Explorer® has the correct proxy settings or use -ProxyAccesstype NoProxyServer if the server does not use a proxy server.
Click Run from the toolbar to confirm the modified script runs successfully (typically this is signified by the console window returning to the PS C:\Windows\System32> prompt).
Click File, Save As to save your script to a local location, e.g. C:\powershellscripts.
Schedule the automated script using Task Scheduler
Open Administrative Tools, Task Scheduler.
Create new task.
Set to run as the administrator user you used above.
Tick the 'Run whether user is logged on or not' box.
Click the Actions tab, then click New and browse to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
Click Open to populate the 'Program/script' field.
In the 'Add arguments (optional)' field, type the path to the script you saved above, e.g. -executionpolicy bypass C:\powershellscripts\AssignProPlusLicence.ps1.
In the 'Start in' field put the folder you saved the scripts to, e.g. C:\powershellscripts.
Click the Triggers tab, select New and set when you want the task to run.
Consider whether you would like to configure the task to stop after running for a set period of time.
Click OK to finish.
Check you have scheduled your task correctly by right-clicking and choosing Run.
Enter your server administrator password when prompted. A result of 0x0 in the Task Scheduler window or logs will confirm that the task has completed successfully.
Checks
If you are new to scheduling and PowerShell scripting, please follow this process using a test PowerShell script that only updates a subset of users or test users, so you can check and confirm the desired results before going on to schedule a PowerShell script that updates a large number of users.
Possible Issues
Set the execution policy in PowerShell
If you cannot run your script manually in PowerShell, you may need to temporarily adjust your execution policy:
Log on to the server with your administrator account.
Open a Windows PowerShell ISE window.
Click View, 'Go to console'.
Type set-executionpolicy remotesigned and press Enter.
Error "cannot find module"
If you see an error "cannot find module" when manually running your script, check if you are running PowerShell as an administrator user and that the Microsoft Azure Active Directory module has been installed. You can type $env:psmodulepath in PowerShell to check the locations where the module should be, e.g. C:\Windows\System32\WindowsPowerShell\v1.0\Modules.
If this article has not helped provide a solution then it is also possible to
log a call...
Document Keywords: pro plus, power shell, a2, a3, o365, 365, office 365, scheduled, TEC4194075, m365