在Powershell中获取当前用户的Microsoft帐户名

时间:2018-12-01 00:06:27

标签: powershell

如何在PowerShell中获取当前用户的Microsoft帐户用户名?我知道如何获取本地用户名(例如$ env:username和其他类似的用户名),而不是Microsoft帐户名(通常是电子邮件地址)。 感谢您的任何帮助! 阿里

2 个答案:

答案 0 :(得分:0)

如果用PlaybackState表示Office 365使用的登录名,则根据您设置O365帐户的方式,您可能需要Microsoft account usernameEmailAddress属性。

使用PowerShell UserPrincipalName模块,您可以这样做:

ActiveDirectory

您也可以通过“ LDAP”方式执行此操作,而无需导入ActiveDirectory模块:

Import-Module ActiveDirectory
$currentUser = Get-ADUser -Identity $env:USERNAME -Properties EmailAddress, UserPrincipalName
$msAccountName = $currentUser.EmailAddress
# or $msAccountName = $currentUser.UserPrincipalName

或类似的东西

$msAccountName = ([ADSI]"LDAP://<SID=$([System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value)>").UserPrincipalName

希望有帮助

答案 1 :(得分:0)

如果要查找您的个人笔记本电脑的信息(域,o365或AD不在域中),请尝试使用这些注册表项。

get-childitem hkcu:\Software\Microsoft\IdentityCRL\UserExtendedProperties\ | sele ct pschildname

Get-ChildItem Registry::HKEY_USERS\.DEFAULT\Software\Microsoft\IdentityCRL\Stored Identities\ | select pschildname

Get-ChildItem Registry::HKEY_USERS\*\Software\Microsoft\IdentityCRL\UserExtendedP roperties\* | select pschildname

我相信Microsoft会捕获您的电子邮件字符串以创建用户帐户,因此您可以执行以下操作来计算当前登录的用户电子邮件:

get-childitem hkcu:\Software\Microsoft\IdentityCRL\UserExtendedProperties\ | where{$_.pschildname -like "*$env:USERNAME*"} | select pschildname

让我知道它是否无效。

相关问题