Win Powershell 获取活跃的本地用户列表

Win Powershell get list of active local users

我想获取 windows 服务器上的活动本地用户列表。当我执行 Get-LocalUser 时,我得到以下输出

Name               Enabled Description                                                              
----               ------- -----------                                                              
DefaultAccount     False   A user account managed by the system.                                    
Guest              False   Built-in account for guest access to the computer/domain                 
labadmin           True    Built-in account for administering the computer/domain                   
Test               True                                                                             
WDAGUtilityAccount False   A user account managed and used by the system for Windows Defender Application Guard scen...

我尝试了 GetLocalUser -Enabled True 并遇到了以下错误

Get-LocalUser : A parameter cannot be found that matches parameter name 'Enabled'.
At line:1 char:79
+ ... ng = New-Object Text.UTF8Encoding $false; Get-LocalUser -Enabled True
+                                                             ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-LocalUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetLocalUserCommandnon-zero return code

过滤掉禁用用户的正确参数是什么?

Get-Help -Name "Get-LocalUser"(也写成help get-localuser)会告诉你唯一可用的参数是:

SYNTAX
    Get-LocalUser [[-Name] <String[]>] [<CommonParameters>]

    Get-LocalUser [[-SID] <SecurityIdentifier[]>] [<CommonParameters>]

并且没有参数可以过滤已启用的用户。您需要取回结果,然后再进行过滤。根据您问题的结果,Enabled 列显示用户对象可能有一个名为 Enabled 的 属性,您可以检查过滤:

$users = Get-LocalUser
$enabledUsers = $users | Where-Object { $_.Enabled }

如果您不知道,或者因为列名称与 属性 名称不完全相同而不起作用,您可以 运行 $users | Get-Member 看看是什么用户对象具有的属性,然后通过它们查找要检查的属性。结果显示已启用:

   TypeName: Microsoft.PowerShell.Commands.LocalUser

Name                   MemberType Definition
----                   ---------- ----------
Clone                  Method     Microsoft.PowerShell.Commands.LocalUser Clone()
Equals                 Method     bool Equals(System.Object obj)
GetHashCode            Method     int GetHashCode()
GetType                Method     type GetType()
ToString               Method     string ToString()
AccountExpires         Property   System.Nullable[datetime] AccountExpires {get;set;}
Description            Property   string Description {get;set;}
Enabled                Property   bool Enabled {get;set;}
FullName               Property   string FullName {get;set;}
LastLogon              Property   System.Nullable[datetime] LastLogon {get;set;}
Name                   Property   string Name {get;set;}
ObjectClass            Property   string ObjectClass {get;set;}
PasswordChangeableDate Property   System.Nullable[datetime] PasswordChangeableDate {get;set;}
PasswordExpires        Property   System.Nullable[datetime] PasswordExpires {get;set;}
PasswordLastSet        Property   System.Nullable[datetime] PasswordLastSet {get;set;}
PasswordRequired       Property   bool PasswordRequired {get;set;}
PrincipalSource        Property   System.Nullable[Microsoft.PowerShell.Commands.PrincipalSource] PrincipalSource {ge...
SID                    Property   System.Security.Principal.SecurityIdentifier SID {get;set;}
UserMayChangePassword  Property   bool UserMayChangePassword {get;set;}