Using bulk actions for renaming Windows devices

A few months ago, I did a blog post about the different ways of renaming Windows 10 devices. This week is a follow-up on that post, as it will also be about renaming Windows devices. This time it’s about using the recently introduced functionality to perform Bulk actions on devices. Those Bulk actions include the action to rename Windows 10 devices in bulk. That Bulk action is also available as a single action on a device and is currently not available for hybrid Azure Active Directory joined devices, nor available for co-managed devices. In this post I’ll show how to perform this action by using the Microsoft Endpoint Manager admin center, followed by using the Microsoft Graph Explorer. I’ll end this post by showing an example using PowerShell and the Microsoft Graph API.

Rename Windows devices using the Microsoft Endpoint Manager admin center

Now let’s start by having a look at using a Bulk action for renaming a Windows 10 device by using the Microsoft Endpoint Manager admin center. This method is – in my opinion – always the first step towards automating an action. The following 9 steps walk through the Bulk action for renaming Windows 10 devices. While performing these steps make sure to use Microsoft Edge and to turn on the Developer tools (Ctrl + Shift + I), as that will help with identifying the Graph request that should be used for automation purposes.

  1. Open the Microsoft Endpoint Manager admin center portal and navigate to Devices All devices > Bulk Device Actions to open the Bulk device actions blade
  2. On the Basics page, provide the following information (see Figure 1) and click Next
  • OS: Select Windows
  • Device action: Select Rename
  • Enter new name: Provide a new naming conform the provided guidelines
  1. On the Devices page, click Select devices to include to select the devices to rename and click Next
  2. On the Review + create page, review the provided information and click Create

Verify request information using the Microsoft Graph Explorer

When following the Bulk action via the Network trace in the Developer tools, it shows the executeAction action that will perform the actual action. The most relevant parts of that action are shown below, as Figure 2 shows the request URL and Figure 3 shows the request payload. That combination is needed for automation purposes.

A closer look shows that the executeAction action is used as the request location to post the request.

https://graph.microsoft.com/beta/deviceManagement/managedDevices/executeAction

That request requires a request body to supply a JSON representation of the different properties. A closer look at that JSON payload shows the properties action, actionName, deviceIds, deviceName, platform, realAction and restartNow. The good thing is that these properties are pretty self explanatory, especially in combination with the action that was performed to retrieve this information. It’s good to specifically point out that the deviceIds property is an array that can currently contain up to a 100 devices and that the deviceName property should contain the naming format for the different applicable devices.

{
	action: "setDeviceName",
	actionName: "setDeviceName",
	deviceIds: ["d8cd02c1-9443-4ad0-8681-937c2e6d7607"],
	deviceName: "CLDCLN%RAND:2%",
	platform: "windows",
	realAction: "setDeviceName",
	restartNow: false
}

The next step toward automating this Bulk action is by trying the correct request URL and request payload via the Microsoft Graph Explorer, as that’s an easy method to try Graph API requests. Simply sign-in, change the action to POST, add the request URL, add the request payload (as shown in Figure 4) and click Run Query.

Note: The application requires the scope DeviceManagementManagedDevices.PrivilegedOperations.All.

Running the query will return a bulkManagedDeviceActionResult result type. That result type provides a JSON representation of the status properties. Those status properties include successfulDeviceIds, failedDeviceIds, notFoundDeviceIds and notSupportedDeviceIds. The good thing is that these properties are also pretty self explanatory. A simple method to verify this behavior is by throwing in some random deviceIds. Those deviceIds should end up as part of the notFoundDeviceIds.

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#microsoft.graph.bulkManagedDeviceActionResult",
    "successfulDeviceIds": [
        "d8cd02c1-9443-4ad0-8681-937c2e6d7607"
    ],
    "failedDeviceIds": [],
    "notFoundDeviceIds": [],
    "notSupportedDeviceIds": []
}

Rename Windows devices using PowerShell and the Microsoft Graph API

After verifying the request URL and the request payload, the last step toward automating this Bulk action is putting it all together in a PowerShell script. I’m going to provide a really simple example that would still require administrator interaction, but does show how it can be achieved. That example is shown below and basically performs the following actions:

  • Install the PowerShell SDK for Microsoft Intune Graph API (if it’s not installed).
  • Connect with the Graph API, which will prompt the administrator for credentials.
  • Set the required variables for the request URL and the request payload.
  • Invoke the Graph API request with the configured variables.
#Install PowerShell SDK for Microsoft Intune Graph API
If ((Get-Module Microsoft.Graph.Intune) -eq $null) {
    Install-Module -Name Microsoft.Graph.Intune
}

#Connect to Microsoft Graph
$ConnectGraph = Connect-MSGraph

#Set the request URL
$URL = "https://graph.microsoft.com/beta/deviceManagement/managedDevices/executeAction"

#Set the JSON payload
$JSONPayload = @"
{
	action: "setDeviceName",
	actionName: "setDeviceName",
	deviceIds: ["d8cd02c1-9443-4ad0-8681-937c2e6d7607"],
	deviceName: "CLDCLN%RAND:2%",
	platform: "windows",
	realAction: "setDeviceName",
	restartNow: false
}
"@

#Invoke the Microsoft Graph request
Try {        
    Invoke-MSGraphRequest -HttpMethod POST -Url $URL -Content $JSONPayload -Verbose -ErrorAction Stop
}
Catch {
    Write-Output "Failed to rename the Windows devices"
} 

When the PowerShell script was successfully executed it will also return the bulkManagedDeviceActionResult result type, as shown in Figure 5. Simple improvements to this PowerShell script would be to remove the administrator interaction, add the deviceIds to a variable and query for the required devices.

More information

For more information about renaming Windows devices and , refer to the following articles:

6 thoughts on “Using bulk actions for renaming Windows devices”

  1. Great post Peter! I’ve been trying to do this and came across your post which pointed me in the right direction. I tried the steps you have for Graph Explorer, but I keep receiving the following error:

    {
    “error”: {
    “code”: “No method match route template”,
    “message”: “No OData route exists that match template ~/singleton/navigation/key with http verb POST for request /DeviceFE/StatelessDeviceFEService/deviceManagement/managedDevices(‘executeAction’).”,
    “innerError”: {
    “date”: “2023-04-03T00:56:59”,
    “request-id”: “fc020580-24b3-4b1b-954a-74d9e9a6c504”,
    “client-request-id”: “d47feb06-5dbf-62d1-fbf1-0e0e7cd1b6a6”
    }
    }
    }

    Any ideas why I might be getting this error?

    Reply
  2. I have noticed that the “deviceName” payload seems to have changed from ONLY the naming scheme to including each device id + the naming scheme. Here’s my output directly from Edge dev tools:
    {
    “action”: “setDeviceName”,
    “platform”: “windows”,
    “deviceIds”: [
    “random-deviceId-first-example”,
    “random-deviceId-second-example”
    ],
    “restartNow”: false,
    “deviceName”: “{\”random-deviceId-first-example\”:\”TEST-%RAND:6%\”,\”random-deviceId-second-example\”:\”TEST-%RAND:6%\”}”,
    “realAction”: “setDeviceName”,
    “actionName”: “setDeviceName”
    }

    I’m trying to figure out how to script that via Powershell now and would love some assistance. Thanks!

    Reply
      • Well, it may be slightly off topic because if manually create a JSON string the way you did using the new deviceName format I mentioned, invoke-msgraphRequest is successful. However, I want to programmatically insert my own values for a bulk rename. My powershell code creates a custom hash that automatically populates multiple deviceIds and deviceNames of the machines I want to rename. I pass the custom hash through convertto-json and it works…but only if there is 1 device to rename. My issue is with Powershell’s Convertto-Json cmdlet. It will not convert the deviceName property into the proper format that I mentioned. The desired json output for deviceName would be: “deviceName”: “{\”xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\”:\”TEST-%RAND:6%\”,\”yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy\”:\”TEST-%RAND:6%\”}”,
        Unfortunately, I cannot produce this result. I’ve tried storing the data into a powershell array and a powershell custom object but, convertto-json always seems to format the data slightly wrong and invoke-msgraphRequest fails.

        Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.