MSEndpointMgr
a picture of fry from futurama looking at intune configurations
Home » Microsoft Endpoint Manager » Intune » Force Intune policy sync from a PowerShell script

Force Intune policy sync from a PowerShell script

Ever wondered how you can kick off a manual or automatic sync of your Intune policies from a PowerShell script?

Not long ago I ran into the need to have policies applied to new devices, a lot quicker than what a normal enrollment does.

Because I had multiple users on shared computers, and a lot of roaming going on, I needed the user enrollment process to be as snappy as possible, but most times it would fail to apply all user policies in the first go, and a reboot might have solved it, but that was unacceptable in this case.

UPDATE: October 21st 2019

Added a separate script to deal with iOS and Android devices in bulk.
This script will get all your iOS and Android devices that are enrolled with Microsoft Intune and signal them to update their MDM policies.

Download Invoke-IntunePolicySyncOniOSAndAndroid.ps1 from GitHub

UPDATE: September 29th 2019

Michael Niehaus (@mniehaus) has answered my prayers for feedback, and boy does he deliver!
In his blog post “Forcing an MDM sync from a Windows 10 client“, Niehaus walks us through his investigation methods, and ends up with a one-liner that solves this whole issue completely (almost).

I will leave the solution below as-is, because it also works from the users context without requiring extra permissions.

The current limitations

So at the moment the only GUI methods that exist to “force” a sync of your policies, is by using the sync button from within the Intune portal,  or from the client – by using the sync button in the Company Portal app or the Work and School account settings page.

manual intune sync
Syncing a device via the Intune portal.

And when I say “force”, I really mean that it will only look for missing or updated policies that haven’t applied – No built-in method exists to have all policies re-evaluate and re-apply.

It is possible to delete the policies and have them reapply by using PowerShell, but that’s not the subject of this article.

The path forward

Keeping in mind that I am working from a pure Intune perspective here (no ConfigMgr available), there was only one course I could see ahead of me, and that was to come up with a PowerShell solution. So this article is really just to tell you it can be done, and I have the Proof-Of-Concept running in production today with good results.

And since it’s not that complex a script, I suggest you read it through from start to end, to really understand what’s going on, as I don’t recommend implementing workarounds like this without fully understanding what it is and does.

Why you might need this workaround

Well as I said, I needed it for a shared PC scenario, but you might also just want to avoid too many reboots during first enrollment, or to further automate your deployment sequence. In any case, I thought I would share it, at least for inspiration and feedback from the community (please use GitHub for code feedback / bugs / updates).

Script overview

So this script essentially does the following:

  • Checks for the Microsoft.Graph.Intune PowerShell Module.
  • Installs / Imports the module.
  • Connects to the Intune Graph.
  • Finds the Device ID based on the hostname of the device you are executing on.
  • Tells Intune to start syncing policies for said device.

Prerequisites

The prerequisites for this to have any chance of working, is that you grant admin consent by running the following bits of PowerShell on your own Windows PC, form an elevated PowerShell prompt.

Install-Module -Name Microsoft.Graph.Intune
Import-Module -Name Microsoft.Graph.Intune
Connect-MSGraph -AdminConsent

If you are unfamiliar with the term “Admin Consent”, I strongly suggest that you read up on it, because this will be come more prevalent in future app´s. Here is a good resource from the creators of all that is holy to us: Admin consent experience

Next thing to do, is download the script and read my suggestions for deployment if you want inspiration. I am sure you will come up with other good ways to use this, so please share those in the comments section for others to read.

Download

Invoke-IntunePolicySync.ps1 on GitHub

Deployment options

Here are a few methods for deploying this, and what I ended up doing in my scenario (spoiler: it’s not very pretty).

  • Deployed as a Device Configuration PowerShell script assigned to the user.
    • This will run only the first time a user logs on, but might not be effective enough if you have many policies etc.
    • Try throwing a DO WHILE loop into the mix that iterates through the script a few times with a few minutes of sleep time in between the iterations.
  • Deployed through GPO as a logon script.
    • This will run every single time a user logs on to the machine, so you might want to place a cookie somewhere on the device to stop it from running all the time.

I ended up with the GPO, since I also had some lingering Group Policies that needed to be forced onto the machine right after a refresh, so I slammed in a “gpupdate /force” and just made the script switch back and forth between the gpupdate and the Intune sync, with a minute of waiting in between.

This works like a charm on newly deployed machines, not only shared computers, and it saves me a reboot or two.
Though for newly deployed machines I just put it in as part of the initial deployment sequence.

But it is ugly as hell, and I might get throttled by Microsoft if I deploy too many machines that try to force a policy sync this often – tho it has yet to be seen.

Try it out and have fun with it.

Don’t forget to subscribe to our RSS feed and follow me on Twitter @michael_mardahl.

(11278)

Michael Mardahl

Michael works as a Cloud Architect with APENTO in Denmark. Specializing in customer journeys from classic Infrastructure to Cloud consumption and is certified as a "Microsoft 365 Enterprise Administrator Expert".
He has been in the IT industry for more than 20 years, and has experience from a broad range of IT projects. When not at work, Michael enjoys the value of spending time with family and friends, and BLOG's passionately about Enterprise Mobility whenever he has time to spare.

8 comments

  • You mentioned that there is a means to delete and re-apply Intune policies on an enrolled device. Do you have any details regarding those commands? Very interested in seeing if such commands could help resolve a policy compliance reporting issue I’ve come across.

  • Hi, can’t you just use this one?
    Get-ScheduledTask -TaskName “Schedule #3 created by enrollment client” | Start-ScheduledTask

    • Hi Pål

      Yes, that is also what Michael Niehaus has posted about, if you read the links in the update section at top of the article.

      But it still won’t work in the context of user if the person does not have admin rights.

  • Michael, we have approx 5000 systems and about 30 different compliance policies. ~4000 devices are failing compliance because of AV or Bitlocker. We want to enable Azure information protection and conditional access so I need to first get all these devices in compliance. I removed the bitlocker and av requirements in all 30 of our compliance policies (i added some settings back in configuration profiles). How can i get Intune to re-evaluate all 5000 systems against the changed policies?
    Thanks in advance.
    Lee

  • Hi Michael,

    Thanks for this post and the script.
    From my point of view the “classic” and even more ugly but more simple way of triggering a sync is to restart the IntuneManagementExtension service.
    Could you comment on why you chose not to do that?

    Cheers
    Klaus

    • Hi Klaus

      Thanks for a great question.

      First of all, the IME service is not a given on any Intune Enrolled Computer, though I am sure most will eventually have it.
      It’s only installed if there are PowerShell scripts or MSI/W32apps assigned to the device.

      Secondly, I have not been able to confirm that the restart of the service actually does a full policy update right away, I think it has different priorities.
      In any case, that was what I was initially doing, but this seemed to work better in my case, and I did not see anyone else sharing much on the matter, so I tought I would try my luck with it, and share the solution.

      Please do a Pull request to the GitHub repo if you have any other cool scripts that can help with Intune Sync 🙂

Sponsors

Subscribe

Do you want to be notified of new posts on our site?

Please enter your email address below:

Categories

MSEndpointMgr.com use cookies to ensure that we give you the best experience on our website.