Computer Information: Simple and Fast



Quite often, you need to quickly, briefly, but informatively get information about a stationary computer or laptop, without additional software and without β€œopening the cover”.

This can be implemented, for example, using the command line tools of Windows or PowerShell.
CMD - time-tested functionality that is in any version of Windows.
In addition, for simple administrative tasks, cmd is more common to use, and somewhere more convenient.

Which is better - CMD or PowerShell? I am not ready to unequivocally answer this question.
However, nothing prevents us from using both of them, it all depends on the task.

We will not collect all information about the PC - for this there are many specialized software!


Implementation using CMD.


We will collect information using the variables of the Windows environment and the execution of WMI scripts.

To display all the environment variables in Windows and their values, use the set command.

To obtain information about the equipment and the system, manage processes and their components, as well as change settings using the capabilities of Windows Management Instrumentation (Windows Management Instrumentation or WMI), use the WMIC command.

You can get a hint on working with the wmic.exe utility by the command:
  • wmic /? - display general help.
  • wmic / ?: BRIEF - display a brief help.
  • wmic / ?: FULL - display full help.


We will use:
  • BASEBOARD (motherboard management);
  • COMPUTERSYSTEM (computer control);
  • CPU (CPU control);
  • DISKDRIVE (physical disk management);
  • MEMORYCHIP (memory chip information).


The script contains many loops with FOR.
A distinctive feature of FOR / F is the ability to work through tokens, as well as support for additional keywords:
  • skip (skipping a certain number of processed lines from the beginning of the file);
  • delims (specify a different delimiter (s), by default, a space and a tab character);
  • tokens (the number of tokens (substrings) received in the loop body and the breakdown limits by separator). You can also set a specific token number that will fall into the first loop variable;
  • usebackq (changing the rules for using quotes inside IN (...)).


Below is a script.
@echo off
:    
set fname=pcinfo.txt
: 
Echo pcname: %computername% >>%fname%
:IP-    
FOR /F "usebackq tokens=2 delims=[]" %%i IN (`ping %Computername% -n 1 -4`) DO if not "%%i"=="" Set ip=%%i
Echo IP %computername%: %ip% >>%fname%
:  
Echo username: %username%  >>%fname%
: 
set cmd=wmic computersystem get model
for /f "skip=1 delims=" %%Z in ('%cmd%') do (
    set _pn=%%Z
	GOTO BREAK1
)
:BREAK1
echo CS Model: %_pn% >>%fname%
:
SETLOCAL ENABLEDELAYEDEXPANSION
set mmr=0
for /f "skip=1 delims=" %%i in ('wmic cpu get name') do (
for /f "tokens=1-2 delims=" %%A in ("%%i") do (
set CPULbl=%%A
set /a mmr=!mmr!+1
echo CPU !mmr!: !CPULbl! >>%fname%
))
: 
set cmd=wmic baseboard get product
for /f "skip=1 delims=" %%Z in ('%cmd%') do (
    set _mb=%%Z
    GOTO BREAK2
)
:BREAK2
echo MB: %_mb% >>%fname%
: 
SETLOCAL ENABLEDELAYEDEXPANSION
set mmr=0
for /f "skip=1 delims=" %%i in ('WMIC MemoryChip get BankLabel^,DeviceLocator^,PartNumber^,Speed^,Capacity') do (
for /f "tokens=1-5 delims=" %%A in ("%%i") do (
set BnkLbl=%%A
set /a mmr=!mmr!+1
echo Memory !mmr!: !BnkLbl! >>%fname%
wmic MEMORYCHIP get banklabel, partnumber, capacity, speed,  manufacturer
))
:
SETLOCAL ENABLEDELAYEDEXPANSION
set mmr=0
for /f "skip=1 delims=" %%i in ('wmic diskdrive get model^,size') do (
for /f "tokens=1-2 delims=" %%A in ("%%i") do (
set HDDLbl=%%A
set /a mmr=!mmr!+1
echo DISK !mmr!: !HDDLbl! >>%fname%
))


Implementation with PowerShell.


In PowerShell, before running the script, you need to run a command that allows the execution of unsigned scripts for the current shell session:
Set-ExecutionPolicy RemoteSigned -Scope Process

Information will be collected using mainly Get-WmiObject -Class win32, everything is simple, working with loops.

We will use:
  • Get-WmiObject -Class win32_processor;
  • Get-WmiObject -Class win32_baseboard;
  • Get-WmiObject Win32_PhysicalMemory;
  • Get-PhysicalDisk;
  • Get-WmiObject -Class Win32_ComputerSystem;
  • Get-WmiObject Win32_NetworkAdapter;
  • Win32_NetworkAdapterConfiguration.


There are minor difficulties in determining the IP address of the active network card. To do this, we use the NetConnectionStatus filter - β€œ2”, after we take the mac address information from the Win32_NetworkAdapter class and the IP address from the Win32_NetworkAdapterConfiguration class and combine it into one request:
PS C:\Users\admin> Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionStatus=2'
ServiceName      : Qcamain10x64
MACAddress       : 58:00:E3:7D:87:3F
AdapterType      : Ethernet 802.3
DeviceID         : 1
Name             : Qualcomm Atheros QCA61x4A Wireless Network Adapter
NetworkAddresses :
Speed            : 144400000

To obtain network parameters for the MACAddress of the active network card, we additionally read Win32_NetworkAdapterConfiguration.

Below is a script.
#    
$fname = "pcinfo.txt"
$CPU = Get-WmiObject  -Class win32_processor
$MB = Get-WmiObject  -Class win32_baseboard
$MEM = Get-WmiObject Win32_PhysicalMemory
$DD = Get-PhysicalDisk
$pcn = Get-WmiObject -Class Win32_ComputerSystem

# 
"pcname:	"+$pcn.Name | Out-File -FilePath $fname -Append -Encoding Default
#IP-    
Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionStatus=2' |
ForEach-Object {
$pcip = 1 | Select-Object IP
$config = $_.GetRelated('Win32_NetworkAdapterConfiguration') 
$pcip.IP = $config | Select-Object -expand IPAddress
$pcip
}
foreach($aip in $pcip) {
"IP:	"+$aip.IP | Out-File -FilePath $fname -Append -Encoding Default
}
#  
"username:	"+$pcn.PrimaryOwnerName | Out-File -FilePath $fname -Append -Encoding Default
# 
"CS Model:	"+$pcn.Model | Out-File -FilePath $fname -Append -Encoding Default
#
$num = 0
foreach($processor in $CPU) {
$num = $num+1
"CPU "+$num+":	"+$processor.Name | Out-File -FilePath $fname -Append -Encoding Default
}
# 
"MB:	"+$MB.Product | Out-File -FilePath $fname -Append -Encoding Default
# 
$num = 0
foreach($memory in $MEM) {
$num = $num+1
"MEMORY "+$num+":	"+$memory.PartNumber+"	"+$memory.Capacity+"	"+$memory.Speed | Out-File -FilePath $fname -Append -Encoding Default
}
#
$num = 0
foreach($disk in $DD) {
$num = $num+1
"DISK "+$num+":	"+$disk.FriendlyName+"	"+$disk.Size+"	"+$disk.MediaType | Out-File -FilePath $fname -Append -Encoding Default
}

All Articles