Schedule PowerShell Script Intune

This is a blog post about how to schedule a PowerShell script via Intune. Of course, we would like this to be built-in within Microsoft Intune but we are not there yet. Hopefully, this will be an option in the future. We would like to schedule app installs and scripts via Microsoft Intune directly.

We do so by using a Win32App to run a Powershell script that registers a scheduled task that runs another Powershell script. Yes, I understand that you would like to maintain as less scripts as possible. That is why the script that does the scheduling is generic and can be used in many use cases. Furthermore, the script that runs via the scheduled task can do anything; “App Installs, Registry Settings & generic configuration”.

Schedule the PowerShell Script via Intune

I have prepared a few files on my Github. You can find them here.

Download all the files and save them to a folder:

You need to change these variables in the files accordingly:

Install.ps1:

All the variables you need are put at the top, change these accordingly:
(I have put the descriptions in the script)

Detection.ps1

Change the “$ScheduledTaskName” variable to the same name as your Install.ps1 file:

Uninstall.ps1

Change the “$ScheduledTaskName” variable to the same name as your Install.ps1 file:

Lastly, the “ScripttoRun.ps1” is the file that the Scheduled Task runs. Enter the code you want to run by the scheduled task. It’s now a dummy that is empty:

Package these files as a Win32App. I assume you know how to do this. If you don’t, please refer to this link.

Use this as the install & uninstall command:

Install:
powershell.exe -ExecutionPolicy Bypass -File .\Install.ps1

Uninstall:
powershell.exe -ExecutionPolicy Bypass -File .\UnInstall.ps1

Use the detection.ps1 as your detection method.

The $ScheduledTime Option

The script also features a switch that helps you define the trigger when the script runs.

This is an example where the “AtLogOn” option is used in the script:

As a result:

This is an example where a specified time is used in the script:

Other Posts:

Install Applications after ESP (Enrollment Status Page)

12 thoughts on “Schedule PowerShell Script Intune”

  1. Hey Niels

    Honestly, I haven’t had the opportunity to try it yet
    and it’s possible that I don’t understanding it either.

    But how can you specify to Intune which win32 app should be executed first?
    Would it not be better to organize this within one PowerShell file (to ?

    Maybe i’m missing something.

    Cheers

    Reply
    • Hi Tom,

      The script is run via the install.ps1 that creates the scheduled task. You can schedule this any way you want.

      Thanks,
      Niels

      Reply
  2. Nice script. You are running the scheduled task as system. Any chance to change that to the current logged on user when it is not an admin user.

    Currently my none admin users can’t create scheduled tasks through powershell, so i need to run the intune script with system instead of user. When i’m using system to install the script i can’t get the currently logged on user. The currently logged on user is needed because i want to write something to the userprofile.

    Reply
  3. Hi Neils,

    How do you prevent the disruptive powershell windows from momentarily opening and closing in front of the individuals signing into the devices when running a powershell script from a scheduled task?

    Reply
  4. Hi Niels and nice idea to schedule stuff easy!

    I was wondering which would be the best way to run the script multiple times per day.

    Thank you

    Reply
    • Hello,

      Thanks for checking my blog 🙂

      The best way is to add triggers to scripts. For example: one at 8:00AM and one 11:00AM

      Hope this helps!

      Niels

      Reply
      • Thank you Niels.

        What would be the best way to edit your script to do that? Or would you suggest using the AtLogOn action? I assume this is every time that you login to your laptop (after shutdown, sleep, etc)? Do you happen to know (from experience) if AtLogOn is consistent given Intune gets sometimes “stuck”?

        Thank you for everything

        Reply
        • Hello,

          I think your suggestion to run it at every logon will be best in your usecase. Then it will probably run multiple times a day.

          It’s just a scheduled task. When it’s deployed it has nothing to do with Intune anymore. So, yeah, I think this is consistent.

          Thanks,
          Niels

          Reply
          • Thank you Niels for your answer

            Btw, you have a typo in the install.ps1 script.

            Start-Transcript -Path “C:\ProgramData\Microsoft\IntuneMsanagementExtension\Logs\$($ScheduledTaskName)_Install.log” -Append

Leave a Comment