可通过 Win32_LogonSession WMI 类获取有关与用户相关联的登录会话的常规信息 :

PS C:\Users\maxsu> Get-CimInstance -ClassName Win32_LogonSession

LogonId    Name LogonType StartTime          Status AuthenticationPackage
-------    ---- --------- ---------          ------ ---------------------
1007194601      2         2020/2/8 8:34:14          NTLM
1007194539      2         2020/2/8 8:34:14          NTLM
910004110       2         2020/2/7 8:26:18          NTLM
910004048       2         2020/2/7 8:26:18          NTLM
812756291       2         2020/2/6 8:23:44          NTLM
812756229       2         2020/2/6 8:23:44          NTLM
696346190       2         2020/2/5 8:54:45          NTLM
696346128       2         2020/2/5 8:54:45          NTLM
555523831       2         2020/2/4 8:32:03          NTLM
555523770       2         2020/2/4 8:32:03          NTLM
459537707       2         2020/2/3 8:43:28          NTLM
459537645       2         2020/2/3 8:43:28          NTLM
360356353       2         2020/2/2 8:09:03          NTLM
360356290       2         2020/2/2 8:09:03          NTLM
278993820       2         2020/2/1 9:02:25          NTLM
278993758       2         2020/2/1 9:02:25          NTLM
247005678       2         2020/1/31 18:35:54        NTLM
247005616       2         2020/1/31 18:35:54        NTLM
216790337       2         2020/1/30 8:14:27         NTLM
216790275       2         2020/1/30 8:14:27         NTLM
126907040       2         2020/1/29 7:33:17         NTLM
126906979       2         2020/1/29 7:33:17         NTLM
787143          2         2020/1/28 8:14:24         NTLM
787058          2         2020/1/28 8:14:24         NTLM

获取登录到计算机的用户

可以使用 Win32_ComputerSystem 显示已登录到特定计算机系统的用户。 此命令将仅返回登录到系统桌面的用户:

PS C:\Users\maxsu> Get-CimInstance -ClassName Win32_ComputerSystem -Property UserName



AdminPasswordStatus         :
BootupState                 :
ChassisBootupState          :
KeyboardPasswordStatus      :
PowerOnPasswordStatus       :
PowerSupplyState            :
PowerState                  :
FrontPanelResetStatus       :
ThermalState                :
Status                      :
Name                        : MAXSU
PowerManagementCapabilities :
PowerManagementSupported    :
Caption                     :
Description                 :
InstallDate                 :
CreationClassName           :
NameFormat                  :
PrimaryOwnerContact         :
PrimaryOwnerName            :
Roles                       :
InitialLoadInfo             :
LastLoadInfo                :
ResetCapability             :
AutomaticManagedPagefile    :
AutomaticResetBootOption    :
AutomaticResetCapability    :
BootOptionOnLimit           :
BootOptionOnWatchDog        :
BootROMSupported            :
BootStatus                  :
ChassisSKUNumber            :
CurrentTimeZone             :
DaylightInEffect            :
DNSHostName                 :
Domain                      :
DomainRole                  :
EnableDaylightSavingsTime   :
HypervisorPresent           :
InfraredSupported           :
Manufacturer                :
Model                       :
NetworkServerModeEnabled    :
NumberOfLogicalProcessors   :
NumberOfProcessors          :
OEMLogoBitmap               :
OEMStringArray              :
PartOfDomain                :
PauseAfterReset             :
PCSystemType                :
PCSystemTypeEx              :
ResetCount                  :
ResetLimit                  :
SupportContactDescription   :
SystemFamily                :
SystemSKUNumber             :
SystemStartupDelay          :
SystemStartupOptions        :
SystemStartupSetting        :
SystemType                  :
TotalPhysicalMemory         :
UserName                    : MAXSU\maxsu
WakeUpType                  :
Workgroup                   :
PSComputerName              :
CimClass                    : root/cimv2:Win32_ComputerSystem
CimInstanceProperties       : {Caption, Description, InstallDate, Name...}
CimSystemProperties         : Microsoft.Management.Infrastructure.CimSystemProperties