批处理文件用户最后一次使用 net user 登录

Batch file user's last logon using net user

我在批处理脚本中使用 net user 命令来查找用户的上次登录时间:

net user administrator | findstr /B /C:"Last logon"

结果如下所示:

Last logon                   04/23/2020 9:02 AM

我只想显示日期和时间并删除 Last logon  

我怎样才能做到这一点?

谢谢。

Windows 10 个 64 位。 PowerShell 5

如何使用 CMD 或 Powershell 显示一个/所有本地用户帐户的上次登录时间戳。如何显示用户名和/或日期和/或时间和/或小时。 None 个命令需要管理员权限。

PowerShell:

# How to display the lastlogon timestamp for one / all local user accounts.
# PowerShell 5. 
# Interactive, searchable, gridview window. All accounts except default accounts named: DefaultAccount, Guest, WDAGUtilityAccount
get-localuser | where {$_.name -notmatch 'defaultaccount|guest|WDAGUtilityAccount'} | select-object lastlogon,name | out-gridview 
# All accounts except default accounts named: DefaultAccount, Guest, WDAGUtilityAccount  
get-localuser | where {$_.name -notmatch 'DefaultAccount|Guest|WDAGUtilityAccount'} | select-object lastlogon,name | format-table -hidetableheaders 
# All local accounts name, date and time. Remove name if not wanted.
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME" 
$adsi.Children | where {$_.SchemaClassName -eq 'user'} | format-table lastlogin,name -HideTableHeaders 
# Account by name:
Get-LocalUser -Name Administrator,_9doug | Select-Object lastlogon,name | Format-Table -HideTableHeaders
# All accounts:
get-localuser | where {$_.name} | select-object lastlogon,name | format-table -hidetableheaders 

交互式、可搜索的网格视图 window:

或者:

Administrator 4/12/2020 7  :  32  :  09 PM
_7doug        11/18/2019 11  :  13  :  53 PM
_8doug        10/25/2019 4  :  47  :  09 PM
_9doug        4/23/2020 6  :  49  :  41 AM

命令:

rem date and time
for /f "tokens=2,*" %g in ('net user administrator ^| findstr /C:"Last logon"') do echo %h
rem only the hour 
for /f "tokens=4" %g in ('net user administrator ^| findstr /C:"Last logon"') do echo Last logon hour was: %g

脚本:

rem date and time
for /f "tokens=2,*" %%g in ('net user administrator ^| findstr /C:"Last logon"') do echo %%h
rem only the hour 
for /f "tokens=4" %%g in ('net user administrator ^| findstr /C:"Last logon"') do echo Last logon hour was: %%g

net user 命令的异常输出:

For

QUSER:仅适用于当前用户。

命令:

rem date and time
for /f "skip=1 tokens=6-8" %g in ('quser administrator') do echo %g %h %i
rem only the hour 
for /f "skip=1 tokens=7 delims=: " %g in ('quser administrator') do echo Last logon hour was: %g

脚本:

rem date and time
for /f "skip=1 tokens=6-8" %%g in ('quser administrator') do echo %%g %%h %%i
rem only the hour 
for /f "skip=1 tokens=7 delims=: " %%g in ('quser administrator') do echo Last logon hour was: %%g

您可以选择使用 WMI 来完成此任务,它应该会为您提供一个普遍可解析的日期和时间字符串。

如果您想要特定用户:

@Set "UsersName=Administrator"
@For /F Tokens^=2^,4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
 Where "Name='%UsersName%'" Assoc /AssocClass:Win32_NetworkLoginProfile 2^>NUL'
)Do @For /F %%I In ('%__AppDir__%wbem\WMIC.exe Path Win32_NetworkLoginProfile^
 Where "Name='%%G\%%H' And LastLogon Is Not Null" Get LastLogon 2^>NUL^
 ^|%__AppDir__%findstr.exe "[0123456789]"')Do @Echo %%H last logon was %%~nI
@Pause

或者,如果您想要用户列表:

@For /F Tokens^=2^,4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
 Assoc /AssocClass:Win32_NetworkLoginProfile 2^>NUL')Do @For /F %%I In (
    '%__AppDir__%wbem\WMIC.exe Path Win32_NetworkLoginProfile Where^
     "Name='%%G\%%H' And LastLogon Is Not Null" Get LastLogon 2^>NUL^
     ^|%__AppDir__%findstr.exe "[0123456789]"')Do @Echo %%H last logon was %%~nI
@Pause

[编辑/?]
基于您在对另一个答案的评论中提出的问题,并且因为我已经指出我的解决方案( s) 上面提供了一个普遍可解析的日期和时间格式,你可以进一步调整它只输出小时。

例如:

@For /F Tokens^=2^,4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
 Assoc /AssocClass:Win32_NetworkLoginProfile 2^>NUL')Do @For /F %%I In (
    '%__AppDir__%wbem\WMIC.exe Path Win32_NetworkLoginProfile Where^
     "Name='%%G\%%H' And LastLogon Is Not Null" Get LastLogon 2^>NUL^
     ^|%__AppDir__%findstr.exe "[0123456789]"')Do @(Set "YYYYmmDDHHMMSS=%%~nI"
     Call Echo(%%H last logged in during the hour of %%YYYYmmDDHHMMSS:~-6,2%%:00)
@Pause

当然,这并不能确定登录发生在哪一天,但是您的附加要求是特定的!