Intune powershell script that gets Windows device compliance state based on UPN list

Picture of Alexandros Manifavas

Alexandros Manifavas

MDM Expert

Share it

 

You can use the following PowerShell script to easily get Windows device compliance state from Intune based on a CSV user principal name list. This Powershell script connects to Microsoft Graph Intune and gets information from Intune Windows devices like “userPrincipalName”, “deviceName”, “complianceState”. Then imports a CSV file with user principal names list and checks if there is a device associated with the user principal names. Finally exports the results in a CSV file.

 

  • Create a CSV file with User Principal Names and give a name like “Users“. Save the file on path: C:\temp

 

 

  • Open an admin PowerShell prompt

<#
.SYNOPSIS
PowerShell script to easily get Windows device compliance state from Intune based on a CSV user principal name list

.DESCRIPTION
This Powershell script connects to Microsoft Graph Intune and gets information from Intune Windows devices like “userPrincipalName”, “deviceName”, “complianceState”.

Then imports a CSV file with user principal names list and checks if there is a device associated with the user principal names.

Finally exports the results in a CSV file.

.PARAMETER
N/A

.EXAMPLE
Example syntax for running the script or function
PS C:\> C:\temp\Export-IntuneDeviceComplianceState.ps1

.NOTES
Filename: Export-IntuneDeviceComplianceState
Author: Alexandros Manifavas
Modified date: 2023-02-19
Version 1.0
#>

# Install PowerShell module for Intune Graph API
Install-Module -Name Microsoft.Graph.Intune

# Connect to Graph API
Connect-MSGraph

# Get list of Windows devices
$MSGraphComputers = Get-IntuneManagedDevice -filter “operatingSystem eq ‘Windows'” | Get-MSGraphAllPages | Select userPrincipalName, deviceName, complianceState

# Import the CSV file with user principal names
$users = Import-Csv -Path “C:\temp\Users.csv”

# Create an array to store the results
$results = @()

# Loop through each user in the CSV file
foreach ($user in $users) {
$userPrincipalName = $user.’User Principal Names’

# Check if there is a device associated with the user
$devices = $MSGraphComputers | Where { $PSItem.userPrincipalName -eq $userPrincipalName }

# Add the user and device information to the results array
if ($devices.count -ne 0) {
foreach ($device in $devices) {
$result = [PSCustomObject]@{
‘User Principal Name’ = $userPrincipalName
‘Device Name’ = $device.deviceName
‘Compliance State’ = $device.complianceState
}

$results += $result

}

}
else {
$result = [PSCustomObject]@{
‘User Principal Name’ = $userPrincipalName
‘Device Name’ = ‘N/A’
‘Compliance State’ = ‘N/A’
}

$results += $result

}

}

# Export the results to a new CSV file
$results | Export-Csv -Path “C:\temp\Results.csv” -NoTypeInformation

 

 

  • A result CSV file will be created on path: C:\temp with the below information.

Related

How to Migrate Print Server

More and more people are migrating from traditional print server to a cloud-hosted printer server. Cloud hosted service is a technology service that uses infrastructure

Read More »