Creating a PowerShell script for remotely inventorying computers involves using PowerShell remoting capabilities to gather information from remote machines. Here’s a basic example script that retrieves system information from remote computers:
# PowerShell script for remotely inventorying computers
# List of remote computer names
$remoteComputers = “RemoteComputer1”, “RemoteComputer2”
# Output file for inventory results
$outputFile = “inventory_results.csv”
# Clear existing inventory file
Remove-Item -Path $outputFile -ErrorAction SilentlyContinue
# Loop through each remote computer
foreach ($computer in $remoteComputers) {
try {
# Use Invoke-Command to run commands on remote computers
$remoteInfo = Invoke-Command -ComputerName $computer -ScriptBlock {
$computerName = $env:COMPUTERNAME
$os = Get-CimInstance -ClassName Win32_OperatingSystem
$processor = Get-CimInstance -ClassName Win32_Processor
$memory = Get-CimInstance -ClassName Win32_PhysicalMemory
$disk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter “DriveType=3”
[PSCustomObject]@{
ComputerName = $computerName
OperatingSystem = $os.Caption
Architecture = $os.OSArchitecture
Processor = $processor.Name
MemoryGB = [math]::Round($memory.Capacity / 1GB, 2)
FreeSpaceGB = [math]::Round($disk.FreeSpace / 1GB, 2)
}
} -Credential (Get-Credential) # Prompt for credentials
# Append the results to the output file
$remoteInfo | Export-Csv -Path $outputFile -Append -NoTypeInformation
} catch {
Write-Host “Failed to retrieve information from $computer. Error: $_”
}
}
Write-Host “Inventory complete. Results saved to $outputFile”
Customize the script:
- Modify the
$remoteComputers
array with the names or IP addresses of your remote computers. - Adjust the
$outputFile
variable to the desired path and filename for storing the inventory results.
This script retrieves basic information such as computer name, operating system, architecture, processor, memory, and free disk space for each remote computer. You can expand or modify the script based on your specific inventory requirements.
Note: PowerShell remoting needs to be enabled on the remote computers, and the user running the script needs appropriate permissions. Additionally, running scripts remotely might require adjusting execution policies on the target machines.
Save the script with a .ps1
extension (e.g., inventory_script.ps1
) and execute it in a PowerShell environment. The script will prompt for credentials to connect to remote computers and save the inventory results to a CSV file.