If user is not local admin then we can use below command to set the value of $UserName and run as system context
$UserName = (Get-ItemProperty (Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Enrollments" -Recurse -ErrorAction SilentlyContinue | % {if((get-itemproperty -Path $_.PsPath) -match "UPN") { $_.PsPath} }) -Name UPN | Select -ExpandProperty UPN).split('@')[0]