This is my personal blog. The opinions and views I express are my own. The information I provide is on an as-is basis. I make no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this blog and will not be liable for any errors, omissions, or delays in this information or any losses, injuries or damages arising from its use.

Computer Inventory script (Powershell)


This inventory script will collect the following data for each machine in livePCs.txt file.

Computer Name
CPU Type
System Type
Build Version
Service Pack Version
Serial Number
IP Address
MAC Address
User Name that last logged in
Date/Time of last login
C: Free Space in GBs
Total Physical Memory
Date/Time of last Reboot


Export a list of computers you want to Inventory from Active Directory, that’s the computers.txt file in the script. The script will test the connection to each machine and export the results to 1 of 2 txt files, livePCs.txt or deadPCs.txt. Once that is complete the inventory will begin using the livePCs.txt file, all the results will be exported to C:\scripts\pcInventory.csv.

Obviously if you don’t have a scripts folder on the C: it will error out.


# On error the script will continue silently without 
$erroractionpreference = "SilentlyContinue"

# TXT file containing the computers your pinging
$testcomputers = gc -Path "C:\scripts\computers.txt"

# Looking through the txt file above and counting computer names.
$test_computer_count = $testcomputers.Length;
$x = 0;

write-host -foregroundcolor cyan ""
write-host -foregroundcolor cyan "Testing $test_computer_count computers, this may take a while."

foreach ($computer in $testcomputers) {
        # I only send 2 echo requests to speed things up, if you want the defaut 4 
        # delete the -count 2 portion
   if (Test-Connection -ComputerName $computer -Quiet -count 2){
        # The path to the livePCs.txt file, change to meet your needs
        Add-Content -value $computer -path c:\scripts\livePCs.txt
        # The path to the deadPCs.txt file, change to meet your needs
        Add-Content -value $computer -path c:\scripts\deadPCs.txt
    $testcomputer_progress = [int][Math]::Ceiling((($x / $test_computer_count) * 100))
	# Progress bar
    Write-Progress  "Testing Connections" -PercentComplete $testcomputer_progress -Status "Percent Complete - $testcomputer_progress%" -Id 1;


write-host -foregroundcolor cyan ""
write-host -foregroundcolor cyan "Testing Connection complete"
write-host -foregroundcolor cyan ""

$ComputerName = gc -Path "C:\scripts\livePCs.txt"

$computer_count = $ComputerName.Length;
# The results of the script are here
$exportLocation = "C:\scripts\pcInventory.csv"
$i = 0;
 foreach ($Computer in $ComputerName){
   $Bios =get-wmiobject win32_bios -Computername $Computer
   $Hardware = get-wmiobject Win32_computerSystem -Computername $Computer
   $Sysbuild = get-wmiobject Win32_WmiSetting -Computername $Computer
   $OS = gwmi Win32_OperatingSystem -Computername $Computer
   $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
   $driveSpace = gwmi win32_volume -computername $Computer -Filter 'drivetype = 3' | 
   select PScomputerName, driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f($_.freespace/1GB)} } |
   Where-Object { $_.driveletter -match "C:" }
   $cpu = Get-WmiObject Win32_Processor  -computername $computer
   $username = Get-ChildItem "\\$computer\c$\Users" | Sort-Object LastWriteTime -Descending | Select Name, LastWriteTime -first 1
   $totalMemory = [math]::round($Hardware.TotalPhysicalMemory/1024/1024/1024, 2)
   $lastBoot = $OS.ConvertToDateTime($OS.LastBootUpTime) 

   #write-host -foregroundcolor yellow "Found $computer"
   $computer_progress = [int][Math]::Ceiling((($i / $computer_count) * 100))
	# Progress bar
    Write-Progress  "Gathering Hardware Info" -PercentComplete $computer_progress -Status "Percent Complete - $computer_progress%" -Id 1;
   foreach ($Network in $Networks) {
    $IPAddress  = $Network.IpAddress[0]
    $MACAddress  = $Network.MACAddress
    $systemBios = $Bios.serialnumber
    $OutputObj  = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $Hardware.Manufacturer
    $OutputObj | Add-Member -MemberType NoteProperty -Name Model -Value $Hardware.Model
    $OutputObj | Add-Member -MemberType NoteProperty -Name CPU_Info -Value $cpu.Name
    $OutputObj | Add-Member -MemberType NoteProperty -Name SystemType -Value $Hardware.SystemType
    $OutputObj | Add-Member -MemberType NoteProperty -Name BuildVersion -Value $SysBuild.BuildVersion
    $OutputObj | Add-Member -MemberType NoteProperty -Name OS -Value $OS.Caption
    $OutputObj | Add-Member -MemberType NoteProperty -Name SPVersion -Value $OS.csdversion
    $OutputObj | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $systemBios
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name UserName -Value $username.Name
    $OutputObj | Add-Member -MemberType NoteProperty -Name Last-Login -Value $username.LastWriteTime
    $OutputObj | Add-Member -MemberType NoteProperty -Name C:_GBfreeSpace -Value $driveSpace.GBfreespace
    $OutputObj | Add-Member -MemberType NoteProperty -Name Total_Physical_Memory -Value $totalMemory
    $OutputObj | Add-Member -MemberType NoteProperty -Name Last_Reboot -Value $lastboot
    $OutputObj | Export-Csv $exportLocation -Append

 write-host -foregroundcolor cyan "Script is complete, the results are here: $exportLocation"


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.