AzureAD/Intune – Add list of systems from CSV to Group with PowerShell

My work has been moving more and more into the cloud and making use of Intune. Recently, I was tasked with adding a large list of systems to a group in AzureAD for use in Intune. No one wants to do something like that manually, so I put together a script to do it for me.

I make use of two different PowerShell modules and you will need you AzureAD tenant ID, the object ID of the group along with rights to add to groups in AzureAD, and a CVS of the systems. I labeled the first row DeviceName.

Install-Module AzureAD
Install-Module msonline

$TenentID = <AzureAD tenent ID>
$groupID = <object ID of the AzureAd group> 
Connect-AzureAD -TenantId $TenentID
Connect-MsolService

$CHGList = Import-Csv -path <path to CSV>
Foreach ($Device in $CHGList)
{
$deviceadd = Get-MsolDevice -Name $Device.DeviceName | Select-Object -ExpandProperty ObjectID 
Add-AzureADGroupMember -ObjectId $groupID -RefObjectId $deviceadd
}