Before deploying, it helps to understand what the script actually does. There are three key actions:
whoami /upn returns the Azure AD User Principal Name of whoever is currently logged in โ for example john.doe@company.com. This means the script works for any user without any hardcoding.
AzureAD\UPN, so the script constructs that string โ e.g. AzureAD\john.doe@company.com โ which is what net localgroup expects.
net localgroup administrators to add the user. Handles three outcomes cleanly: success, already a member (no error), or failure โ with a clear message for each.
Here is the full script:
# Get the Azure AD UPN of the logged-in user $UPN = (whoami /upn).Trim() # Build the AzureAD identity string $AzureADUser = "AzureAD\$UPN" Write-Host "Detected AzureAD User: $AzureADUser" # Add to local Administrators group $Result = net localgroup administrators "$AzureADUser" /add 2>&1 if ($LASTEXITCODE -eq 0) { Write-Host "SUCCESS: '$AzureADUser' added to Administrators group." -ForegroundColor Green } elseif ($Result -match "already a member") { Write-Host "INFO: '$AzureADUser' is already in Administrators group." -ForegroundColor Yellow } else { Write-Host "ERROR: Failed to add '$AzureADUser'. Details: $Result" -ForegroundColor Red }
whoami /upn returns the correct Azure AD UPN. Make sure this is set correctly in Intune โ covered in Step 3.
Copy the script above into a plain text editor (Notepad, VS Code) and save it with a .ps1 extension.
Add-SignedInUser-LocalAdmin.ps1Upload the script to Intune and configure the correct execution settings.
Add SignedIn User to Local AdminDescription: Detects the Azure AD signed-in user and adds them to the local Administrators group.
Click Next.
Add-SignedInUser-LocalAdmin.ps1 file, then set the following:
| Setting | Value |
|---|---|
| Run this script using the logged on credentials | Yes โ |
| Enforce script signature check | No |
| Run script in 64-bit PowerShell host | Yes โ |
whoami /upn will not return a valid Azure AD UPN and the script will fail.
Trigger a manual sync on the target device to speed up script delivery rather than waiting for the standard check-in cycle.
Get-ScheduledTask | Where-Object {$_.TaskName -like "*PushLaunch*"} | Start-ScheduledTask
After sync, check the script execution log to confirm it ran:
# IME agent execution log โ check for your script name and result
C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\AgentExecutor.log
Confirm the signed-in user has been successfully added to the local Administrators group using any of these methods.
AzureAD\ account listed under Administrators:Get-LocalGroupMember -Group "Administrators"
Expected output (the AzureAD account should appear):
ObjectClass Name PrincipalSource ----------- ---- --------------- User AzureAD\john.doe@company.com AzureAD User BUILTIN\Administrator Local
net localgroup administrators
C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\AgentExecutor.log โ search for your script name. If the log shows the script ran as SYSTEM, go back to Step 3 and confirm Run using logged on credentials is set to Yes.