Rename multiple devices in Intune with Graph and a CSV file

In this blog post I would like to introduce a script to rename the device names in Intune based on a CSV file.

We use the following tools:

  • Microsoft Graph
  • Microsoft Excel

You can download the script here: GitHub

The PowerShell Code (you have to add the path to the .csv file in the $CSVFile variable):

# Check if Graph PowerShell module is present, install if not
if (!(Get-Module -Name Microsoft.Graph.Intune)) {
    Install-Module -Name Microsoft.Graph.Intune -Scope CurrentUser -Force -Verbose
}

# Connect to Graph
Connect-MSGraph

# Path to CSV File, Example: "C:\TEMP\device_rename.csv"
$CSVFile = "<PATH TO CSV>"
$CSVFilePath = Split-Path $CSVFile -Parent

# Path to logfile
$LogFile = "$CSVFilePath\Intune_Rename_Script\intune_rename_script.log"
$LogDirectory = Split-Path $CSVFile -Parent
$LogDirectoryFullPath = $LogDirectory + "\Intune_Rename_Script\"

# Create the directory if it doesn't exist

if (!(Test-Path $LogDirectoryFullPath)) {
    New-Item -ItemType Directory -Path $LogDirectoryFullPath | Out-Null
}

# Import the CSV file
$DeviceList = Import-Csv $CSVFile -Delimiter ";"

$deviceCounter = 0


# Loop through each device in the list
foreach ($Device in $DeviceList) {
    # Get the device information
    $SerialNumber = $Device.SerialNumber
    $NewDeviceName = $Device.Devicename

    Write-Output $SerialNumber
    Write-Output $NewDeviceName

    # Get the Intune device with matching serial number
    $IntuneDevice = Get-IntuneManagedDevice -Filter "SerialNumber eq '$SerialNumber'"

    # Rename the device if it's found
    if ($IntuneDevice) {
        # Check if the current device name matches the new device name
        if ($IntuneDevice.DeviceName -ne $NewDeviceName) {
            $DeviceId = $IntuneDevice.id
            $Resource = "deviceManagement/managedDevices('$DeviceID')/setDeviceName"
            $GraphApiVersion = "Beta"
            $URI = "https://graph.microsoft.com/$graphApiVersion/$($resource)"
            $JSONPayload = @"
    {
    deviceName:"$NewDeviceName"
    }
"@
            Write-Output $JSONPayload
            Invoke-MSGraphRequest -HttpMethod POST -Url $uri -Content $JSONPayload -Verbose -ErrorAction Continue

            $deviceCounter++

            Write-Output "$deviceCounter devices has been renamed."
            Write-Output "$deviceCounter devices has been renamed." | Out-File $LogFile -Append
        }
        else {
            Write-Output "Device with serial number $SerialNumber is already named $NewDeviceName."
            Write-Output "Device with serial number $SerialNumber is already named $NewDeviceName." | Out-File $LogFile -Append
        }
    }
    else {
        Write-Output "Device with serial number $SerialNumber was not found."
        Write-Output "Device with serial number $SerialNumber was not found." | Out-File $LogFile -Append
    }
}

Invoke-Item -Path $LogFile

What is Microsoft Graph and how does it work with Intune?

Microsoft Graph is a REST API endpoint for accessing data and insights from the Microsoft cloud. When used in combination with Intune, Microsoft Graph enables developers to build custom solutions that can interact with Intune and access data such as device management, app protection, and compliance information. This integration allows organizations to automate and streamline various mobile device and app management tasks, and to gain deeper insights into device security and compliance. By leveraging the power of Microsoft Graph, organizations can enhance their overall mobile device and app management experience with Intune and create custom solutions that meet their unique needs.

How can devices be renamed in Intune?

There are many ways to rename devices (Intune Portal, Powershell.) But what if I wanted to automate this instead of renaming each device individually? What if I have a .csv file that contains the serial numbers and device names that I want to rename in Intune? With this goal at hand, I wrote this script that should help you also to rename multiple devices at once.

How should my .csv file look like?

In the script the following delimiter “;” is required. Theoretically you can change this to any other delimiter. Make sure that the file has the following format:

DevicenameSerialnumber
YOUR DEVICENAMEYOUR SERIALNUMBER

If you open it in a texteditor like Notepad++ it should look like this with ; as the delimiter:
Devicename;SerialNumber
YOUR DEVICENAME; YOUR SERIALNUMBER

Privileges to run the script

The user running the script needs to have the necessary privileges to install the Microsoft.Graph.Intune module and to make API requests to Microsoft Graph. This can typically be achieved by having the necessary permissions in Azure Active Directory (AAD) to manage devices using Microsoft Intune, as well as access to the Microsoft Graph API. The exact privileges required will depend on the Azure AD configuration and the security requirements of the organization.

How does the script work?

The most important part is that only matching serialnumbers in the csv and in intune will be checked for the device name. If they both don´t match, then nothing will be changed. This will prevent you to mistakenly rename any devices.

  1. The first section checks if the Microsoft.Graph.Intune PowerShell module is already installed, and if not, it installs the module.
  2. Then it connects to Microsoft Graph API.
  3. Next, the script sets the path to the CSV file with the device information to be renamed and to the log file.
  4. The directory for the log file is created if it does not exist.
  5. The script then imports the CSV file with the device information.
  6. It starts a loop to process each device in the list and to rename the device if necessary.
  7. The script gets the device information (serial number and new device name) from the CSV file.
  8. It retrieves the device from Intune using the Get-IntuneManagedDevice cmdlet.
  9. If the device is found, the script checks if the current name of the device matches the new name.
  10. If the names are different, the script sends a request to Microsoft Graph API to update the device name.
  11. The device counter is incremented and the output is logged in the log file.
  12. If the device is not found, an error message is logged in the log file.
  13. Finally, the log file is opened.

Conclusion

If you follow me on Twitter or do read my blogs, then you know that I love automation. This was the easiest way to rename multiple devices. I hope this helps you as much as it did help me. You can message me on Twitter if you got any questions: Twitter

Disclaimer:
The explanations inside the dropdown menus are made with Chat-GPT and fact checked through me.

1 thought on “Rename multiple devices in Intune with Graph and a CSV file”

Comments are closed.