Share via


Software inventory via Powershell

Last week we receive a question from a customer to get an software overview of all laptops. When we put all our laptops into computers.txt and you run the PS script, we receive a clear html overview

#Declaring Parameters
     $Computers = Get-Content C:\Install\computers.txt
     $array = @()
     foreach($PC in $Computers){
     $computername=$PC.computername
     }
     #param(
     #[array]$arrComputer="$env:computername"
     #)$$
      
#variables 
$date = (Get-Date).ToShortDateString()
$filename = "InventoryReport"
$vUserName = (Get-Item env:\username).Value
$vComputerName = (Get-Item env:\Computername).Value
#$filepath = (Get-ChildItem env:\userprofile).value

## Html Style
$a = "<style>"
$a = $a + "BODY{background-color:Lavender ;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:thistle}"
$a = $a + "TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
$a = $a + "</style>"
# removing old HTML Report if exists
if (test-Path C:\Install\filename.html) { remove-Item c:\Install\filename.html;
Write-Host -ForegroundColor white -BackgroundColor Red    "Old file removed"
}
# Running Command 

foreach ( $name in $Computers ) {
write-Host "Testing Network Connection with $name" -ForegroundColor Green
if ( Test-Connection $name -Count 2 -Quiet ) {
write-Host "Getting software information." -ForegroundColor Magenta -BackgroundColor White 
ConvertTo-Html -Title "Software Inventory" -Body "<h1> Computer Name : $name </h1>" >>  "C:\install\filename.html"
Get-WmiObject Win32_ComputerSystemProduct -ComputerName $name | Select Vendor,Version,Name,IdentifyingNumber | ConvertTo-HTML -Head $a -Body "<H2> System Information </H2>"  >> "C:\Install\filename.html"
    Get-WmiObject Win32_Bios -Computername $name | Select SerialNumber | ConvertTo-HTML -Head $a -Body "<H2> Bios Information </H2>"  >> "C:\Install\filename.html"
    Get-WmiObject win32_NetworkAdapter -ComputerName $name | Select Name,Manufacturer,Description ,AdapterType,Speed,MACAddress,NetConnectionID | ConvertTo-HTML -Head $a -Body "<H2> NetworkCard Information </H2>"  >> "C:\Install\filename.html"
    Get-WmiObject win32_OperatingSystem -ComputerName $name | Select Caption, ServicePackMajorVersion  | ConvertTo-HTML -Head $a -Body "<H2> OS Information </H2>"  >> "C:\Install\filename.html"
    Get-WmiObject win32_OperatingSystem -ComputerName $name | Select @{n='LastBootTime';e={$_.ConvertToDateTime($_.LastBootUpTime)}} | ConvertTo-HTML -Head $a -Body "<H2> Last Reboot Information </H2>"  >> "C:\Install\filename.html"
    Get-WmiObject win32_Product -ComputerName $name | Select Name,Version,Installdate,Vendor,PackageName | Sort Name -Descending | ConvertTo-html -Head $a -Body "<H2> Software Installed</H2>" >> "C:\Install\filename.html"  
} else {
write-Host " $name is not found or not reachable." -ForegroundColor white -BackgroundColor Red
}
}
## Opening file and the file 
$Report = "The Report is generated On  $(get-date) by $((Get-Item env:\username).Value) on computer $((Get-Item env:\Computername).Value)"
$Report  >> "C:\Install\filename.html" 
write-Host "file is saved in "C:\Install\ and the name of file is $filename.html" -ForegroundColor Cyan
invoke-Expression "C:\Install\filename.html" 

Hope it help!