检索当前域用户的全名

Jon*_*oux 26 windows powershell

使用 PowerShell,如何在不需要 ActiveDirectory 模块的情况下获取当前登录的域用户的全名(不仅是其用户名)?

Cla*_*ton 30

$dom = $env:userdomain
$usr = $env:username
([adsi]"WinNT://$dom/$usr,user").fullname
Run Code Online (Sandbox Code Playgroud)

返回:

John Doe
Run Code Online (Sandbox Code Playgroud)

其他一些(主要是)晦涩的属性也可用。几个有用的:

  • Homedrive UNC
  • Homedrive 信件
  • 描述
  • 登录脚本

尝试:

[adsi]"WinNT://$dom/$usr,user" | select *
Run Code Online (Sandbox Code Playgroud)

  • 好答案。当然,这 * 是 * 查询 AD... :) (4认同)

The*_*ner 16

我喜欢接受的答案,但只是因为我想自己尝试一下:

$user = whoami
Get-WMIObject Win32_UserAccount | where caption -eq $user | select FullName
Run Code Online (Sandbox Code Playgroud)

返回:

FullName
--------
TheCleaner
Run Code Online (Sandbox Code Playgroud)

或者,如果您希望没有标题信息而只是结果:

$user = whoami
Get-WMIObject Win32_UserAccount | where caption -eq $user | select FullName | ft -hide
Run Code Online (Sandbox Code Playgroud)

  • @squillman 不是,只是给清洁工带来了困难。 (4认同)
  • 但不需要 AD PS 模块 (3认同)

MDM*_*313 7

一个使用 Powershell 3.0 的衬垫:

gwmi win32_useraccount | where {$_.caption -match $env:USERNAME} | select fullname | ft -HideTableHeaders
Run Code Online (Sandbox Code Playgroud)