Functions/Set-AutopilotComputernameAndGrouptag.ps1
function Set-AutopilotComputernameAndGrouptag { <# .SYNOPSIS Set computernames and grouptags in Windows Autopilot from a CSV file .DESCRIPTION Use if you want to set computernames to something else instead of the serialnumber and random number options available in Intune. This function can also set grouptags Parameter CsvFile can be a local file or a URL Example template for CSV file: SerialNumber,AssetTag,GroupTag ABC1234,Laptop1,SharedLaptop DEF5678,Computer1,SharedDesktop XYZ9876,Laptop2,PersonalLaptop QWE1289,Laptop3,PersonalLaptop If you don't want to set either Computername or grouptag, you can leave the column blank or remove it completely. .NOTES .LINK .EXAMPLE Set-AutopilotComputernameAndGrouptag -CsvFile https://storageaccount.blob.core.windows.net/intune/DevicesSerialNumberAndAssetTag.csv -ComputernamePrefix "Custcode-" -StartAutopilotSync Sets computername and grouptag based on the Azure storage CSV file with prefix "Custcode-" (example: "Custcode-Laptop1") and starts the Autopilot sync process. .EXAMPLE Set-AutopilotComputernameAndGrouptag -CsvFile https://storageaccount.blob.core.windows.net/intune/DevicesSerialNumberAndAssetTag.csv -ComputernamePrefix "Company-" -WhatIf Runs the command but does not change any computernames or grouptags in Autopilot. #> [CmdletBinding()] param ( [Parameter()] [string] $ComputernamePrefix, [Parameter()] [string] $ComputernameSuffix, [Parameter()] [string] $CsvFile, [Parameter()] [switch] $StartAutopilotSync, [Parameter()] [switch] $WhatIf ) if (-not(Get-MgUser -ea 0)) { Write-Error "MgGraph not connected, please connect with Connect-MgGraph" break } if ((Get-MgProfile).Name -ne "beta") { Select-MgProfile beta } if (-not(Get-MgDeviceManagementWindowAutopilotDeviceIdentity -Top 1)) { Write-Error "No Autopilot devices found" break } $Config = @{ Prefix = $ComputernamePrefix Suffix = $ComputernameSuffix SourceCSVFile = $CsvFile LocalCSVFile = "$($env:TEMP)\DevicesSerialNumberAndAssetTag.csv" } $Autopilotdevices = Get-MgDeviceManagementWindowAutopilotDeviceIdentity -All # $Autopilotdevices | Format-Table Remove-Item $Config.LocalCSVFile -Force -ea 0 Start-BitsTransfer -Source $Config.SourceCSVFile -Destination $Config.LocalCSVFile -ErrorAction Stop $DeviceImport = Import-Csv $Config.LocalCSVFile # $DeviceImport | Format-Table foreach ($ID in $DeviceImport) { $CurrentAutopilotDevice = $Autopilotdevices | Where-Object serialNumber -EQ $ID.SerialNumber # $CurrentAutopilotDevice foreach ($c in $CurrentAutopilotDevice) { if ($ID.Grouptag) { if ($c.groupTag -ne $ID.Grouptag) { if (-not($WhatIf)) { # Set-AutopilotDevice -id $($c.id) -groupTag $($ID.Grouptag) Update-MgDeviceManagementWindowAutopilotDeviceIdentityDeviceProperty -WindowsAutopilotDeviceIdentityId $($c.id) -GroupTag $($ID.Grouptag) } Write-Output "$($ID.SerialNumber) - Update-MgDeviceManagementWindowAutopilotDeviceIdentityDeviceProperty -WindowsAutopilotDeviceIdentityId $($c.id) -GroupTag $($ID.Grouptag)" } } if ($ID.AssetTag) { $NewComputerName = "$($Config.Prefix)$($ID.AssetTag)$($Config.Suffix)" if ($c.displayName -ne $NewComputerName) { if (-not($WhatIf)) { # Set-AutopilotDevice -id $($c.id) -displayName $NewComputerName Update-MgDeviceManagementWindowAutopilotDeviceIdentityDeviceProperty -WindowsAutopilotDeviceIdentityId $($c.id) -DisplayName $($NewComputerName) } Write-Output "$($ID.SerialNumber) - Update-MgDeviceManagementWindowAutopilotDeviceIdentityDeviceProperty -WindowsAutopilotDeviceIdentityId $($c.id) -DisplayName $($NewComputerName)" } } } } if ($StartAutopilotSync) { if (-not($WhatIf)) { Sync-MgDeviceManagementWindowAutopilotSetting } } Remove-Item $config.LocalCSVFile -Force -ErrorAction 0 ### } |