Azure | Intune: Powershell to generate template to be used in Bulk operations in Device group
Background
As an Intune Admin, We know how to create device group in Azure AD or Intune. We know how to add and remove devices from device group. This manual step is helpful only when you need to add or remove few devices. But what would you do when you need to remove 50-100 devices. Now, this is where you don’t want to go manual way. Here, use of Bulk operation under device group will be helpful.
Limitation
When you will try to do so, you get to import the CSV (template) containing list in some format available to preview and download. In short, now your csv should contain device object. This would have been more easier if Microsoft would have given option to add device names directly instead of device object and to pull out device object for all device names and to feed this in CSV file.
Solution
In this post we will prepare a PowerShell Script that will intake device names as a CSV list and it will generate the CSV of collection of device object IDs in required template format.
Steps to bulk import group members
- Sign in to the Azure or MEM portal.
- Select Groups > All groups. Search for required group.
- Open the group to which you need to add members.
- Select Import members under Bulk operations.
- Now, create your own CSV file that would contain device names as shown below:-
- Run the below powershell script to fetch Object IDs of provided device names given in step 5.
Install-module -name msonline -force
Connect-MsolService
$CHGList = Import-Csv -path "C:\temp\MachineNames.csv"
$ObjectID = @(
@{ Name = 'version:v1.0'; Expression = { $_.'ObjectId' } }
)
'*'| Select-Object -Property $ObjectID |Export-CSV -Path "C:\temp\testx.csv" -NoTypeInformation -Append -Force
ForEach ($displayName in $CHGList) {
Get-MsolDevice -Name $displayName.displayName | Select-Object -Property $ObjectID |Export-CSV -Path "C:\temp\BulkUploadtemplate.csv" -NoTypeInformation -Append
}
MachineNames.csv –> Create a device list, column name should be displayName and mention all required devices names under this. And place this csv in C:\temp location. testx.csv –> Creates template (Done by script), you can ignore this. BulkUploadtemplate.csv –> Creates required template (Done by script)
–> On the Bulk import group members page, Upload BulkUploadtemplate.csv file.
–> Select Submit to start the Azure bulk operation that imports the group members to the group.
–> After sometime, you will see a notification saying the bulk operation succeeded.
Troubleshooting Reference
If BulkUploadtemplate.csv is not working then please download the template and copy object IDs from this CSV and try to import it.
Check import status
You can see the status of all of your pending bulk requests in the Bulk operation results page.
Note: Each bulk activity to import a list of group members can run for max one hour.
Similarly, you can use this script for removing bulk members from Device group.