PowerShell中提取systeminfo命令结果中指定信息的方法求助
Hey Dale, great question! The issue here is that systeminfo outputs plain text lines instead of structured PowerShell objects (like how Get-ADUser returns objects with properties you can access directly). That's why trying $s."System Boot Time" doesn't work—there's no such property on a string array.
Here are a couple of straightforward ways to get the System Boot Time you need:
Method 1: Directly match and extract the line
This is quick if you only need one specific piece of info:
# First capture the systeminfo output $s = systeminfo /s majorsys01 # Find the line containing "System Boot Time" $bootTimeLine = $s | Select-String "System Boot Time:" # Split the line at the colon, take the second part, and trim extra spaces $bootTime = $bootTimeLine -split ":", 2 | Select-Object -Index 1 | ForEach-Object { $_.Trim() } # Output the result $bootTime
The Select-String finds the right line, -split ":", 2 breaks it into two parts (so we don't split on any colons in the time itself), then we clean up the whitespace with Trim().
Method 2: Convert the output to a structured object
If you want to easily access multiple pieces of info later, convert the text output into a PowerShell object first:
$s = systeminfo /s majorsys01 # Filter out empty lines, convert "Label: Value" to "Label=Value", then turn into a custom object $systemInfoObject = $s | Where-Object { $_ -match ": " } | ForEach-Object { $_ -replace ": ", "=" } | ConvertFrom-StringData | ForEach-Object { [PSCustomObject]$_ } # Now you can access properties just like Get-ADUser! $systemInfoObject."System Boot Time"
Now you can grab other details too, like $systemInfoObject."OS Version" or $systemInfoObject."Total Physical Memory" without writing new matching logic.
Hope that solves your puzzle! 😊
备注:内容来源于stack exchange,提问作者Dale

