Windows: administrator command-line cheat sheet (users, AD, network, inventory)
Handy cmd one-liners to administer Windows machines and Active Directory: sessions, accounts, groups, domain, inventory, firewall, IP.
Command-line (cmd) cheat sheet for everyday administration of Windows machines and an Active Directory: sessions, users, groups, accounts, domain, inventory, firewall and IP checks. Each is a one-liner and can be reused in batch files (with FOR /F to extract the substrings you need). Many date from the Windows 2000/XP/2003 era: NBTSTAT, NETSH Firewall, DSTools and WMIC are obsolete or removed in recent Windows versions; with PowerShell, prefer the equivalent cmdlets.
Sessions and logged-on users
Number of sessions open on a server (locally, native commands):
NET SESSION | FIND /C "\\"
Remotely, with PsExec (Sysinternals PSTools):
PSEXEC \\servername NET SESSION | FIND /C "\\"
Without /C, the command lists the sessions instead of counting them.
Who is logged on to a remote computer:
WMIC /Node:remotecomputer ComputerSystem Get UserName
PSLOGGEDON -L \\remotecomputer
PSEXEC \\remotecomputer NET CONFIG WORKSTATION | FIND /I " name "
NBTSTAT -a remotecomputer | FIND "<03>" | FIND /I /V "remotecomputer"
WMICis the most reliable native method (WMI must be enabled on the remote machine).PSLOGGEDONreturns the last logged-on user if nobody is currently logged on.NBTSTATis very fast but does not always return a name, and no longer exists in recent Windows versions.- Another approach is to query the remote registry (
REG QUERY \\computer\HKU,Volatile Environmentkey of each SID); the syntax depends on theREG.EXEversion.
Last logon of a local account:
net user LocAdmin | findstr /B /C:"Last logon"
Active Directory accounts and groups
Find a login from a name, or a full name from a login (DSTools from Windows Server 2003 onward):
DSQUERY USER -name *lastname* | DSGET USER -samid -display
DSQUERY USER -samid *loginname* | DSGET USER -samid -display
NET USER loginname /DOMAIN | FIND /I " name "
The NET USER variant needs neither AD nor DSTools, but it is language dependent: on a non-English Windows, adjust the string searched by FIND.
Groups a user belongs to, including nested membership (useful to check access, since permissions are usually granted to domain local groups that contain global groups):
DSQUERY USER -samid loginname | DSGET USER -memberof -expand
A user's permissions on a directory (SHOWACLS, Windows Server 2003 Resource Kit):
CD /D d:\directory2check
SHOWACLS /U:domain\userid
Last password change and account state (active or locked):
NET USER loginname /DOMAIN | FIND /I "Password last set"
NET USER loginname /DOMAIN | FIND /I "Account active"
Re-enable an account, with or without a new password:
NET USER loginname /DOMAIN /ACTIVE:YES
NET USER loginname newpassword /DOMAIN /ACTIVE:YES
Reset a password:
NET USER loginname newpassword /DOMAIN
DSQUERY USER -samid loginname | DSMOD USER -pwd *
With *, the password is not displayed on screen: you are prompted twice and typing is not echoed.
Local account password that never expires (TRUE to make it expire):
WMIC.EXE /Node:remotecomputer Path Win32_UserAccount Where Name="user" Set PasswordExpires="FALSE"
Domain and network
NET VIEW /DOMAIN
NET VIEW
FOR /F "skip=3 delims=\ " %%A IN ('NET VIEW') DO ECHO.%%A
The last command prints names only; delims is a backslash followed by a tab and a space.
NETDOM (Windows 2000 Support Tools):
NETDOM QUERY /D:MyDomain DC
NETDOM QUERY /D:MyDomain PDC
NETDOM QUERY /D:mydomain.com FSMO
NETDOM QUERY /D:MyDomain SERVER
NETDOM QUERY /D:MyDomain WORKSTATION
NETDOM /DOMAIN:MyDomain MEMBER \\computer2Bdeleted /DELETE
In order: domain controllers, PDC, FSMO role holders (Active Directory only), member servers, workstations, delete a computer account.
Domain controllers with DSTools (host names only with -o rdn):
DSQUERY Server
DSQUERY Server -o rdn
Certificate server: certutil -config - -ping, or look at the AD group "Cert Publishers".
Inventory
Disk space for every server listed in servers.txt (one name per line), output as CSV (SRVSPACE.CSV must not exist or be empty):
FOR /F %%A IN (servers.txt) DO (
WMIC /Node:%%A LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size /Format:csv | MORE /E +2 >> SRVSPACE.CSV
)
Installed drivers, locally or remotely:
DRIVERQUERY /V /FO CSV > %ComputerName%.csv
DRIVERQUERY /S remote_PC /V /FO CSV > remote_PC.csv
Printers on a machine, and local administrators:
WMIC /Node:remote_PC Path Win32_Printer Get DeviceID
NET LOCALGROUP Administrators
Check whether a given update (KB) is installed:
wmic qfe get hotfixid | find "KB99999"
wmic qfe | find "KB99999"
Get-WmiObject -query 'select * from win32_quickfixengineering' | foreach {$_.hotfixid}
Windows Firewall
Disable the firewall only while the machine is connected to the domain:
NETSH Firewall Set OpMode Mode = DISABLE Profile = DOMAIN
Disable it completely (not recommended, unless an enterprise firewall replaces it):
SC [ \\Remote_computer ] Stop SharedAccess
SC [ \\Remote_computer ] Config SharedAccess start= disabled
Is IPv4 or IPv6 supported?
PING 127.0.0.1 | FIND "TTL=" >NUL 2>&1
IF ERRORLEVEL 1 (ECHO IP v4 NOT supported) ELSE (ECHO IP v4 supported)
PING ::1 | FINDSTR /R /C:"::1:[^$]" >NUL 2>&1
IF ERRORLEVEL 1 (ECHO IP v6 NOT supported) ELSE (ECHO IP v6 supported)
A faster variant is WMIC Path Win32_PingStatus WHERE "Address='127.0.0.1'" Get StatusCode; status code 0 means success.
Event log: interactive logons
XML filter (Event Viewer, "Custom View", XML tab): event 4624 with logon type 2 (interactive) or 10 (remote desktop).
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">*[System[(EventID=4624)]] and *[EventData[Data[@Name='LogonType'] and (Data=2 or Data=10)]]</Select>
</Query>
</QueryList>
Notes
NBTSTAT,NETSH Firewall,WMICand DSTools are legacy; current equivalents are PowerShell cmdlets (Get-ADUser,Get-CimInstance,Get-NetFirewallProfile...).NET USERoutput depends on the system language.- Reference: PSTools (Sysinternals).