-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-SystemInfo.ps1
More file actions
98 lines (80 loc) · 2.72 KB
/
Copy pathGet-SystemInfo.ps1
File metadata and controls
98 lines (80 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<#
.SYNOPSIS
Collects basic system information for IT support and documentation purposes.
.DESCRIPTION
Gathers operating system, CPU, memory, disk, network, and uptime details
and outputs them as a structured PowerShell object. Optionally exports
the results to a text or JSON file.
.PARAMETER OutputPath
Optional. If provided, the script will write the output to the specified file.
The extension determines the format:
- .txt = formatted list
- .json = JSON
.EXAMPLE
.\Get-SystemInfo.ps1
.EXAMPLE
.\Get-SystemInfo.ps1 -OutputPath .\systeminfo.txt
.EXAMPLE
.\Get-SystemInfo.ps1 -OutputPath .\systeminfo.json
#>
param(
[string]$OutputPath
)
# OS info
$os = Get-CimInstance Win32_OperatingSystem
$osName = $os.Caption
$osVersion = $os.Version
# CPU info
$cpu = Get-CimInstance Win32_Processor | Select-Object -First 1
$cpuName = $cpu.Name
$cpuCores = $cpu.NumberOfCores
$cpuLogical = $cpu.NumberOfLogicalProcessors
# Memory info
$totalMemoryGB = [math]::Round($os.TotalVisibleMemorySize / 1MB, 2)
$freeMemoryGB = [math]::Round($os.FreePhysicalMemory / 1MB, 2)
$usedMemoryGB = [math]::Round($totalMemoryGB - $freeMemoryGB, 2)
# Disk info (system drive)
$systemDrive = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'"
$diskSizeGB = [math]::Round($systemDrive.Size / 1GB, 2)
$diskFreeGB = [math]::Round($systemDrive.FreeSpace / 1GB, 2)
# Network adapters (enabled, with IP)
$netAdapters = Get-NetIPConfiguration |
Where-Object { $_.IPv4Address -ne $null } |
Select-Object InterfaceAlias, @{Name="IPv4";Expression={$_.IPv4Address.IPAddress}}
# Uptime
$lastBoot = $os.LastBootUpTime
$uptime = (Get-Date) - $lastBoot
$uptimeString = "{0} days, {1} hours, {2} minutes" -f `
[int]$uptime.Days, `
[int]$uptime.Hours, `
[int]$uptime.Minutes
# Build result object
$result = [PSCustomObject]@{
OSName = $osName
OSVersion = $osVersion
CPUName = $cpuName
CPUCores = $cpuCores
CPULogical = $cpuLogical
TotalMemoryGB = $totalMemoryGB
UsedMemoryGB = $usedMemoryGB
FreeMemoryGB = $freeMemoryGB
DiskSizeGB = $diskSizeGB
DiskFreeGB = $diskFreeGB
Uptime = $uptimeString
NetworkAdapters = $netAdapters
}
# Output to console
$result
# Optional export
if ($OutputPath) {
$extension = [System.IO.Path]::GetExtension($OutputPath).ToLower()
switch ($extension) {
".json" {
$result | ConvertTo-Json -Depth 4 | Out-File -FilePath $OutputPath -Encoding UTF8
}
default {
$result | Format-List | Out-File -FilePath $OutputPath -Encoding UTF8
}
}
Write-Host "System information written to $OutputPath" -ForegroundColor Green
}