可以通过查询环境变量查询当前用户名:
PS> $env:userdomain baozhen-PC PS> $env:username baozhen
如果要获取更多的信息,例如用户的S ID,用户组的ID,可以通过.NET方法。
PS> [System.Security.Principal.WindowsIdentity]::GetCurrent()
AuthenticationType : NTLM
ImpersonationLevel : None
IsAuthenticated    : True
IsGuest            : False
IsSystem           : False
IsAnonymous        : False
Name               : baozhen-PCbaozhen
Owner              : S-1-5-21-261666072-2831887403-1966525179-1001
User               : S-1-5-21-261666072-2831887403-1966525179-1001
Groups             : {S-1-5-21-261666072-2831887403-1966525179-513, S-1-1-0, S-1-5-21-261666072-283
                     1887403-1966525179-1008, S-1-5-32-545...}
Token              : 1148
PS> [System.Security.Principal.WindowsIdentity]::GetCurrent() |
 Select-Object -ExpandProperty Name
baozhen-PCbaozhen
PS> [System.Security.Principal.WindowsIdentity]::GetCurrent() |
 Select-Object -ExpandProperty User
BinaryLength AccountDomainSid                 Value
------------ ----------------                 -----
          28 S-1-5-21-261666072-2831887403... S-1-5-21-261666072-2831887403...
PS> [System.Security.Principal.WindowsIdentity]::GetCurrent() |
 Select-Object -ExpandProperty Groups
 BinaryLength AccountDomainSid                 Value
------------ ----------------                 -----
          28 S-1-5-21-261666072-2831887403... S-1-5-21-261666072-2831887403...
          12                                  S-1-1-0
          28 S-1-5-21-261666072-2831887403... S-1-5-21-261666072-2831887403...
          16                                  S-1-5-32-545
          12                                  S-1-5-4
          12                                  S-1-2-1
          12                                  S-1-5-11
          12                                  S-1-5-15
          12                                  S-1-2-0
          16                                  S-1-5-64-10
本文链接: https://www.pstips.net/powershell-who-am-i.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
                                      请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
