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/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().
2
u/BlackV Apr 28 '26 edited Apr 28 '26
get-datelike 3 times when once would do (-formatparameter or format operator-f)confirm[System.Environment]::xxxworks as expected in linuxworks fineGet-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 editorSelect-String 'Model name'instead ofSelect-String "Model"?Write-Host2
u/BlackV Apr 28 '26
you have used inline code, not a code block
it'll format it properly OR
Inline code block using backticks
`Single code line`inside normal textSee 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] * 1kbwill 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
-matchand-splitto turn things into the right small string, then turn them into the right type.Putting it all together into an object (Raspberry Pi only):
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
u/[deleted] Apr 28 '26
[removed] — view removed comment