如何用PowerShell准确检测Windows10本地账户是否关联微软账户
检测Windows 10本地账户是否关联微软账户的PowerShell方法
你之前使用的命令都是检测本地安全主体账户的类型,而用户通过Office登录关联的微软账户属于账户的在线身份关联状态,需要读取系统存储的关联身份信息来判断。
方法1:读取注册表检测当前登录用户的关联微软账户
- 获取当前用户的SID:
$currentSid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
- 读取微软账户关联信息的注册表项:
$msaRegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\Credential Providers\{D6886603-9D2F-4EB2-B667-1971041FA96B}\$currentSid" if (Test-Path $msaRegistryPath) { $msaDetails = Get-ItemProperty -Path $msaRegistryPath Write-Host "当前用户已关联微软账户: $($msaDetails.UserName)" } else { Write-Host "当前用户未关联微软账户" }
方法2:读取用户身份存储信息检测
该方法会列出当前用户存储的所有关联身份,包括微软账户:
$storedIdentities = Get-Item -Path "HKCU:\Software\Microsoft\IdentityCRL\StoredIdentities" foreach ($identity in $storedIdentities.GetSubKeyNames()) { if ($identity -match "@outlook.com|@hotmail.com|@live.com|@microsoft.com") { Write-Host "检测到关联的微软账户: $identity" } }
方法3:通过账户配置服务查询
使用Get-CimInstance查询用户的在线账户关联状态:
$userProfiles = Get-CimInstance -ClassName Win32_UserProfile | Where-Object { $_.SID -eq $currentSid } $msaAssociated = Get-CimInstance -ClassName MSFT_UserAccount -Namespace root/cimv2/microsoft/windows/userprofile | Where-Object { $_.ProfileSID -eq $currentSid } if ($msaAssociated) { Write-Host "已关联微软账户: $($msaAssociated.UserName)" } else { Write-Host "未关联微软账户" }
说明:以上方法中,方法1和方法2直接读取系统存储的关联信息,准确性较高;方法3依赖WMI类,部分系统版本可能需要调整命名空间。
内容的提问来源于stack exchange,提问作者y y
相关产品推荐
相关产品推荐

