Autocodewizard Logo Practical Project - Automated System Inventory - Autocodewizard Ebooks - PowerShell Essentials: Mastering Scripting and Automation for Windows Administration

Chapter 12: Practical Project - Automated System Inventory

Apply your PowerShell skills to create an automated system inventory script that collects data about installed software, hardware specifications, and system settings.

In this chapter, we�ll create a practical project: an automated system inventory script. This script gathers essential information about a system, including hardware specifications, installed software, and configuration settings, and outputs the results for easy review. Automating inventory collection can help administrators maintain accurate system records.

Project Overview

The goal of this project is to create a PowerShell script that gathers system data and stores it in a structured format, such as a CSV file. This script will cover:

Collecting Hardware Information

To retrieve hardware information, PowerShell provides several cmdlets that access details about CPU, memory, and disk space. For example:

# CPU information
$cpuInfo = Get-WmiObject -Class Win32_Processor | Select-Object -Property Name, NumberOfCores, MaxClockSpeed

# Memory information
$memoryInfo = Get-WmiObject -Class Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum | Select-Object @{Name='TotalMemoryGB';Expression={[math]::round($_.Sum / 1GB, 2)}}

# Disk space information
$diskInfo = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" | Select-Object -Property DeviceID, Size, FreeSpace

This section of the script retrieves CPU details, total memory in GB, and disk information for each logical drive.

Gathering Installed Software Details

To list installed software, use the registry to access relevant data. For example:

$softwareInfo = Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" |
    Select-Object -Property DisplayName, DisplayVersion, Publisher, InstallDate

This command retrieves the names, versions, publishers, and install dates of installed applications from the registry.

Retrieving System Settings

Collecting system settings, such as OS version and network configuration, provides context for the inventory. For example:

# OS version
$osInfo = Get-WmiObject -Class Win32_OperatingSystem | Select-Object -Property Caption, Version, OSArchitecture

# Network configuration
$networkInfo = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = True" | Select-Object -Property Description, MACAddress, IPAddress, DefaultIPGateway

This portion of the script retrieves the OS version and network details for IP-enabled network adapters.

Saving Inventory Data to CSV

Once the data is collected, it can be saved in a CSV file for easy access and reporting. Use Export-Csv to export data:

$cpuInfo | Export-Csv -Path "C:\Inventory\CPU_Info.csv" -NoTypeInformation
$memoryInfo | Export-Csv -Path "C:\Inventory\Memory_Info.csv" -NoTypeInformation
$diskInfo | Export-Csv -Path "C:\Inventory\Disk_Info.csv" -NoTypeInformation
$softwareInfo | Export-Csv -Path "C:\Inventory\Software_Info.csv" -NoTypeInformation
$osInfo | Export-Csv -Path "C:\Inventory\OS_Info.csv" -NoTypeInformation
$networkInfo | Export-Csv -Path "C:\Inventory\Network_Info.csv" -NoTypeInformation

This example saves each category of data in a separate CSV file within the C:\Inventory directory.

Scheduling the Inventory Script

To automate inventory collection, schedule the script to run regularly using Task Scheduler. Refer to Chapter 11 for guidance on scheduling scripts in Task Scheduler, allowing for recurring inventory updates without manual intervention.

Summary and Next Steps

In this chapter, we created an automated system inventory script that collects and exports data on hardware, installed software, and system settings. This project demonstrates how PowerShell can streamline system management tasks. In the next chapter, we�ll dive into advanced PowerShell techniques, exploring concepts like scripting with arrays, working with complex data structures, and implementing functions for enhanced modularity.