PowerShell Welcome Message

When you SSH into a Linux system, you will often get a welcome message showing the version, memory usage, processes, network information, etc. In this blog post, I will show you how to use one in each PowerShell session with some lovely system details that might be useful.

What does a Linux welcome message look like?

There are many formats, but I use Ubuntu a lot, and when I SSH into an Ubuntu server, I see something like this: (I changed the IP addresses a bit. This is from my VPS.)

Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-91-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Fri Dec 15 07:35:56 PM CET 2023

  System load:                      0.09423828125
  Usage of /:                       67.6% of 75.04GB
  Memory usage:                     56%
  Swap usage:                       0%
  Processes:                        239
  Users logged in:                  0
  IPv4 address for br-22412d75b633: 192.168.251.1
  IPv4 address for br-2536347f8210: 192.168.251.129
  IPv4 address for docker0:         172.16.0.1
  IPv4 address for eth0:            10.10.10.10
  IPv6 address for eth0:            2a01:ac1:2a36:7a27::1

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

Expanded Security Maintenance for Applications is enabled.

0 updates can be applied immediately.


You have no mail.
Last login: Tue Dec 12 11:31:13 2023 from 192.168.1.183

What details are in my welcome message script?

I haven’t added all the details yet like I wanted. Getting a good system load number from Windows was an excellent exercise in getting counters, and I lost that match for now 😉 But my script will output this for the moment, working on improving it in the future so that it matches the amount of detail the Ubuntu one displays:

Things I ran into when creating the script

As I mentioned, getting specific details can be difficult. I’m using several variables and PSCustomObjects, but formatting them nicely isn’t easy. I resorted to using `t (Tabs) to get the data in a nice format. I tried putting it into one big PSCustomObject or a JSON file, but I was not too fond of the output.

Thanks, Kelly, for adding the System load and Swap usage part in the Comments! Added it to the script!

Using the script

You can save the contents of the script to c:\scripts\Show-Systeminfo.ps1, for example, and add it to your PowerShell profile like this when you’re in a PowerShell session:

- notepad $profile
- Add ". c:\scripts\Show-Systeminfo.ps1"
- Save/Quit your profile
- Start a new PowerShell session

The script

Below are the contents of the script. Please save it to c:\scripts, for example, and add it to your profile.

#Variables
$OS = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption, Version
$CPU = $((Get-CimInstance -ClassName Win32_Processor).name)
$Disks = foreach ($disk in Get-CimInstance -Class win32_logicaldisk) {
  [PSCustomObject]@{
    Drive = $disk.DeviceID
    Total = [math]::Round($disk.Size / 1GB, 2)
    Free  = [math]::Round($disk.FreeSpace / 1GB, 2) 
  }
}
$Memory = "$(Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | ForEach-Object {"{0:N2}" -f ([math]::round(($_.Sum/1GB),2))})Gb/$([math]::round((Get-CIMInstance Win32_OperatingSystem).FreePhysicalMemory / 1024 / 1024, 2))Gb"
$Processes = (Get-Process).count
$Networkadapters = foreach ($adapter in Get-NetAdapter | Where-Object Status -eq Up | Sort-Object Name, Type) {
  foreach ($ipinterface in Get-NetIPAddress | Where-Object InterfaceAlias -eq $adapter.Name) {
    [PSCustomObject]@{
      Adapter = $adapter.name
      Type    = $ipinterface.AddressFamily
      Address = $ipinterface.IPAddress
    }
  }
}
$Usersloggedin = (Get-CimInstance -Query "select * from win32_process where name='explorer.exe'").ProcessID.count
$SystemLoad = (Get-Counter "\Processor(_Total)\% Processor Time").CounterSamples.CookedValue
$swapUsage = (Get-Counter "\Paging File(_Total)\% Usage").CounterSamples.CookedValue

#Screen ouput
Write-Host "Welcome to the $($host.Name) of $($env:COMPUTERNAME) ($($OS.Caption) $($OS.Version))`n" -ForegroundColor Green
Write-Host "System information as of $(Get-Date -Format 'dd-MM-yyyy HH:MM')`n" -ForegroundColor Green
Write-Host "CPU:`t`t`t`t$($CPU)" -ForegroundColor Green
Write-Host "System load:`t`t`t$("{0:N2}%" -f $systemLoad)" -ForegroundColor Green
foreach ($disk in $Disks) {
  Write-Host "Disk $($disk.Drive) Total/Free:`t`t$($disk.Total)/$($disk.free)" -ForegroundColor Green
}
Write-Host "Memory usage (Total/Free):`t$($Memory)" -ForegroundColor Green
Write-Host "Swap usage:`t`t`t$("{0:N2}%" -f $swapUsage)" -ForegroundColor Green
Write-Host "Processes:`t`t`t$($Processes)" -ForegroundColor Green
Write-Host "Users logged in:`t`t$($Usersloggedin)" -ForegroundColor Green
foreach ($adapter in $Networkadapters | Sort-Object Adapter, Type) {
  Write-Host "Adapter $($adapter.Adapter):`t`t$($adapter.Type) - $($adapter.Address)" -ForegroundColor Green
}

Download the script(s) from GitHub here.

3 thoughts on “PowerShell Welcome Message

  1. $systemLoad = (Get-Counter “\Processor(_Total)\% Processor Time”).CounterSamples.CookedValue
    Write-Host “System load:tt`t$(“{0:N2}%” -f $systemLoad)” -ForegroundColor Green

    $swapUsage = (Get-Counter “\Paging File(_Total)\% Usage”).CounterSamples.CookedValue
    Write-Host “Swap usage:tt`t$(“{0:N2}%” -f $swapUsage)” -ForegroundColor Green

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.