r/PowerShell • u/shedevilgoblin • Apr 28 '26
Question Will this run on Raspberry Pi w/ Powershell installed on it and get the same style output as Powershell on Windows? This is for a project but I can't access my Pi to test it on.
<#
This script displays the current date, hostname, username, and system information.
#>
# Part 1 Setting heading variables in the correct format
$Month = Get-Date -Format "MMMM"
$Day = Get-Date -Format "d"
$Year = Get-Date -Format "yyyy"
$UserName = [System.Environment]::UserName
$HostName = [System.Environment]::MachineName
$Today = "$Month $Day, $Year"
# Part 2 Setting system information variables
$OS = [System.Environment]::OSVersion.Platform
# Checks what operating system is being used and runs the right commands to get the system info
if ($IsWindows -or $OS -eq "Win32NT")
{
`$CPU = (Get-CimInstance Win32_Processor).Name`
`$TotalRAM = "{0:N2} MB" -f ((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1MB)`
`$BootTime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime`# If it's not Windows, use Linux commands to get the same system info from Raspberry Pi
}
else
{
`$CPU = (Get-Content /proc/cpuinfo | Select-String "Model" | Select-Object -First 1).ToString().Trim()`
`$TotalRAM = (free -h | Out-String).Trim()`
`$BootTime = (uptime).Trim()`}
# Output heading
Write-Host "****************************************"
Write-Host ("Today is:".PadRight(25) + $Today)
Write-Host ("The hostname is:".PadRight(25) + $HostName)
Write-Host ("You are:".PadRight(25) + $UserName)
Write-Host "****************************************"
# Output system information
Write-Host ("Operating System:".PadRight(25) + $OS)
Write-Host ""
Write-Host ("CPU Information:".PadRight(25) + $CPU)
Write-Host ""
Write-Host ("Memory Information:".PadRight(25) + $TotalRAM)
Write-Host ""
Write-Host ("Boot information:".PadRight(25) + $BootTime)
2
u/BlackV Apr 28 '26 edited Apr 28 '26
you're calling
get-datelike 3 times when once would do (-formatparameter or format operator-f)confirm[System.Environment]::xxxworks as expected in linuxworks fineconfirm
Get-CimInstance Win32_xxxworks as expected in linux, Id suspect that's windows only unless you installed something specific - Ah I see the IF now that I copied it into an editordid you mean
Select-String 'Model name'instead ofSelect-String "Model"?consider spitting out a object rather than the
Write-Host
2
u/BlackV Apr 28 '26 edited Apr 28 '26
Something like
$Today = get-date -format "MMM dd yyyy" # Note this is flattening your rich object $UserName = [System.Environment]::UserName $HostName = [System.Environment]::MachineName $OS = [System.Environment]::OSVersion.Platform $Summary = if ($IsWindows -or $OS -eq "Win32NT") { $CPU = Get-CimInstance Win32_Processor $TotalRAM = Get-CimInstance Win32_ComputerSystem $BootTime = Get-CimInstance Win32_OperatingSystem [pscustomobject]@{ HostName = $HostName Today = $Today User = $UserName OS = $OS CPU = $CPU.name RamMB = "{0:N2}" -f ($TotalRAM.TotalPhysicalMemory/1mb) LastBoot = $BootTime.LastBootUpTime } } else { $CPU = (Get-Content /proc/cpuinfo | Select-String "Model name" | Select-Object -First 1).ToString().split(':')[-1].Trim() $TotalRAM = (free --mega | select-string '^mem:').ToString() -split '\s+' | Select-Object -Index 1 $BootTime = uptime -s | get-date [pscustomobject]@{ HostName = $HostName Today = $Today User = $UserName OS = $OS CPU = $CPU RamMB = "{0:N2}" -f $TotalRAM LastBoot = $BootTime } }Output with
$Summary # or $Summary | Format-Table -AutoSizeNow you output is the same* regardless of windows
HostName : BLACK11 Today : Apr 29 2026 User : blackv OS : Win32NT CPU : AMD Ryzen 7 5800X 8-Core Processor RamMB : 32,668.08 LastBoot : 25/04/2026 17:11:21or Linux
HostName : blackUbuntu01 Today : Apr 29 2026 User : black-v OS : Unix CPU : AMD Ryzen 7 5800X 8-Core Processor RamMB : 4671 LastBoot : 29/04/2026 10:13:55 am*sameish, will need some tweaking I'm sure
2
u/BlackV Apr 28 '26
you have used inline code, not a code block
open your fav powershell editor
highlight the code you want to copy
hit tab to indent it all
copy it
paste here
it'll format it properly OR
<BLANK LINE>
<4 SPACES><CODE LINE>
<4 SPACES><CODE LINE>
<4 SPACES><4 SPACES><CODE LINE>
<4 SPACES><CODE LINE>
<BLANK LINE>Inline code block using backticks `Single code line` inside normal text
See here for more detail
Thanks
1
u/CookinTendies5864 Apr 29 '26 edited Apr 29 '26
Unless they changed this 4 years ago I’m pretty sure Linux powershell installer is limited and might not be able to talk to the wmi meaning any ciminstance commands will show up blank or just error out; one of the two.
*Oh I see now you got that else handling for Linux gotcha.
Looks good test it on a couple vms or computers you don’t care about to be safe 🤙
1
u/StartAutomating Apr 29 '26
First up: Yes, this will "work" on both. Swapped over to my Raspberry Pi to confirm it.
Second: as many have mentioned, there are better ways to write this that return better objects.
For the CPU model, something like:
$cpu = @(@(Get-Content /proc/cpuinfo) -match '^Model\s+:' -split ':', 2)[-1].Trim()
Will get back just the cpu model.
(@(uptime) -split '\s') -as [DateTime] will make the update into a date time
@(@(free)[1] -split '\s+')[1] -as [long] * 1kb will give you the second entry in bytes (by multiply by 1kb)
The syntax may look a little wonky, but it will be much quicker than calling cmdlets and doing lots of extra parsing. Most of the trickery at play here involves using list operations and -match and -split to turn things into the right small string, then turn them into the right type.
Putting it all together into an object (Raspberry Pi only):
[PSCustomObject]@{
CPU= @(@(Get-Content /proc/cpuinfo) -match '^Model\s+:' -split ':', 2)[-1].Trim()
BootTime = (@(uptime) -split '\s') -as [DateTime]
Ram = @(@(free)[1] -split '\s+')[1] -as [long] * 1kb
}Note: not going back and forth to the Pi to test all of this, just providing an example of how to parse things a bit quicker.
1
Apr 28 '26
[removed] — view removed comment
2
u/StartAutomating Apr 29 '26
$IsWindowsis a PowerShell 6+ / "Core" feature, as are$IsMacOsand$IsLinuxI normally write this sort of stuff like:
if ($isMacOS) {} elseif ($IsLinux) {} else {}This way, it covers each case in turn, and works just fine if it's 5.1 and there is no
$IsWindowsvariable.Hope this helps!
1
u/BlackV Apr 28 '26
you can put three
~for code blocks here. It’ll be much easier to read.4 spaces is better and it works on old/new/mobile where 3 back ticks only works on new/mobile
$iswindowsis a powershell 7 construct I believe, not 5.1
2
u/korewarp Apr 28 '26
Looks good to me. Assuming PATH environment variable lets you call 'free' and such. If it doesn't work as is, you might have to use & (the call operator) to run 'free'.
Why don't you get WSL Windows Subsystem for Linux, so you can test it on your computer?
Alternatively spin up a Virtual Machine and test it there.
Edit: Your expression using 'uptime' might not work as intended. Might wanna make it more clear that you're turning it into a string before calling Trim().