Windows and Active Directory: useful commands for administrators
Command-line memo for administering an Active Directory domain: sessions, accounts, groups, domain controllers, inventory.
A command-line memo for day-to-day administration of an Active Directory domain and Windows machines: sessions, accounts, groups, domain controllers, inventory. Based on the list by Rob van der Woude. Many of these commands date from Windows XP / 2003; on current systems prefer the PowerShell ActiveDirectory module (Get-ADUser, Get-ADComputer, Get-ADDomainController...).
Logon server
echo %logonserver%
$env:LOGONSERVER
if ($env:LOGONSERVER -match "MicrosoftAccount") { "Logged on with Windows account" }
Logon scripts are served from the NETLOGON share of the domain controller (for example \\dc01\NETLOGON).
Sessions and logged-on users
Number of sessions open on a file server (remove /C to get the list):
NET SESSION | FIND /C "\\"
PSEXEC \\servername NET SESSION | FIND /C "\\"
User logged on to a remote computer:
WMIC /Node:remotecomputer ComputerSystem Get UserName
PSLOGGEDON -L \\remotecomputer
NBTSTAT -a remotecomputer | FIND "<03>" | FIND /I /V "remotecomputer"
WMICis the most reliable native method (WMI must be enabled on the target).NBTSTATis the fastest but does not always return a user name.PSLOGGEDON(Sysinternals) returns the last logged-on user when nobody is currently logged on.
Accounts and groups
DS tools (Windows Server 2003 and later):
DSQUERY USER -name *lastname* | DSGET USER -samid -display
DSQUERY USER -samid *loginname* | DSGET USER -samid -display
DSQUERY USER -samid loginname | DSGET USER -memberof -expand
-memberof -expand expands group membership recursively, which is useful to check access to a resource.
With NET USER:
NET USER loginname /DOMAIN | FIND /I " name "
NET USER loginname /DOMAIN | FIND /I "Password last set"
NET USER loginname /DOMAIN | FIND /I "Account active"
The strings searched by FIND depend on the Windows display language: adapt them on a non-English system.
Permissions of a user on a directory (SHOWACLS, Windows Server 2003 Resource Kit):
CD /D d:\directory2check
SHOWACLS /U:domain\userid
Password and lockout
:: Reset a password
NET USER loginname <PASSWORD> /DOMAIN
DSQUERY USER -samid loginname | DSMOD USER -pwd <PASSWORD>
:: Unlock an account (with or without a new password)
NET USER loginname /DOMAIN /ACTIVE:YES
NET USER loginname <PASSWORD> /DOMAIN /ACTIVE:YES
With DSMOD, replacing the password with * avoids displaying it: you are prompted twice to type it.
Local accounts, remotely through WMIC:
:: Password never expires
WMIC.EXE /Node:remotecomputer Path Win32_UserAccount Where Name="user" Set PasswordExpires="FALSE"
:: Password expires
WMIC.EXE /Node:remotecomputer Path Win32_UserAccount Where Name="user" Set PasswordExpires="TRUE"
:: Force a change at next logon (do not combine with "never expires")
WMIC /Node:remotecomputer Path Win32_UserAccount Where Name="user" Set PasswordExpires="TRUE"
NET USER "user" /LogonPasswordChg:YES
Domains, computers and domain controllers
:: Domains and workgroups, computers on the network
NET VIEW /DOMAIN
NET VIEW
:: Domain controllers
NETDOM QUERY /D:contoso.com DC
DSQUERY Server
DSQUERY Server -o rdn
:: Primary domain controller / FSMO roles
NETDOM QUERY /D:contoso.com PDC
NETDOM QUERY /D:contoso.com FSMO
:: Member servers and workstations
NETDOM QUERY /D:contoso.com SERVER
NETDOM QUERY /D:contoso.com WORKSTATION
:: Delete a computer account
NETDOM /DOMAIN:contoso.com MEMBER \\computer2Bdeleted /DELETE
NETDOM is part of the Windows 2000 Support Tools (nowadays RSAT tools). To list only the names from NET VIEW:
FOR /F "skip=3 delims=\ " %%A IN ('NET VIEW') DO ECHO.%%A
delims is a backslash, followed by a tab and a space.
Disk space across several servers
With a SERVERS.TXT file (one server name per line) and a SRVSPACE.CSV file that does not exist or is 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
)
The CSV holds, for each partition of each server: server name, drive, file system, free space, size.
Machine inventory
:: Installed drivers (local or remote)
DRIVERQUERY /V /FO CSV > %ComputerName%.csv
DRIVERQUERY /S remote_PC /V /FO CSV > remote_PC.csv
:: Printers
WMIC /Node:remote_PC Path Win32_Printer Get DeviceID
:: Default printer
WMIC Path Win32_Printer Where Name='printername' Call SetDefaultPrinter
:: Local administrators
NET LOCALGROUP Administrators
:: Installed updates (DISM returns more detail than WMIC)
DISM /Online /Get-Packages
WMIC QFE List
Network and firewall
:: Is IPv4 supported?
PING 127.0.0.1 | FIND "TTL=" >NUL 2>&1
IF ERRORLEVEL 1 (ECHO IP v4 NOT supported) ELSE (ECHO IP v4 supported)
:: Disable the firewall for the domain profile only
NETSH Firewall Set OpMode Mode = DISABLE Profile = DOMAIN
:: Stop the firewall completely (not recommended)
SC \\Remote_computer Stop SharedAccess
SC \\Remote_computer Config SharedAccess start= disabled
DHCPLOC (Windows Server 2003) detects rogue DHCP servers: pass the legitimate DHCP servers as arguments.
Notes
WMICand most of the tools listed here are deprecated on recent Windows; prefer the PowerShell equivalents (Get-CimInstance,Get-ADUser,Get-HotFix...).- Disabling Windows Firewall should only be considered when an equivalent enterprise firewall is in place.
- Passwords in the examples are replaced by
<PASSWORD>.