首页 > 解决方案 > Powershell get-aduser 格式化 Write-Host

问题描述

我有一个 powershell 脚本,我希望运行并输出格式为:

PS C:\Users\Administrator> Get-ADuser j.citizen


DistinguishedName : CN=John Citizen,CN=Users,DC=mydomain,DC=local
Enabled           : True
GivenName         : John
Name              : John Citizen
ObjectClass       : user
ObjectGUID        : 3ecc1da4-6443-4e77-b594-3061be28a434
SamAccountName    : j.citizen
SID               : S-1-5-21-3281199732-950135425-2894168244-1115
Surname           : Citizen
UserPrincipalName : j.citizen@mydomain.local

我的 Powershell 代码是:

$varAccount = Get-AdUser j.citizen
Write-Host "$varAccount"

它输出:

CN=John Citizen,CN=Users,DC=mydomain,DC=local

标签: powershell

解决方案


然后看看Format-List

$varAccount = Get-AdUser j.citizen
$varAccount | Format-List -Property *

您还可以在变量中捕获此控制台输出,以将其保存在文本文件中,例如:

$output = $varAccount | Format-List -Property * | Out-String

推荐阅读