Windows Server How-To

How To Use PowerShell To Retrieve Basic System Information

Also create a script that will allow you to pick and choose which information you want to receive.

Last night I was playing around with some of the Sysinternals tools and rediscovered a tool called Bginfo. For those who might not be familiar with Sysinternals, it is a collection of free tools provided by Microsoft. The tools tend to be geared primarily toward diagnostic and troubleshooting tasks.

At any rate, the BgInfo tool, which you can see in Figure 1, displays basic system information on the Windows desktop. As I looked at this tool, it occurred to me that it is relatively easy to get the same information from PowerShell (although I do not know how to make PowerShell display text on the desktop).

[Click on image for larger view.]  Figure 1. The Bginfo tool displays basic system information on the desktop.

As you can see in the figure above, Bginfo displays the boot time, the CPU, the default gateway, the DHCP server, the DNS server, the amount of free space, the host name, the Internet Explorer version, the IP address, the logon domain, the logon server, the MAC address, the machine domain, the free memory, the network card, the network speed, the network type, the OS version, the service pack level, the snapshot time, the subnet mask, the system type, the user name and some volume information.

Needless to say, this is a lot of information and we are going to have to use multiple PowerShell cmdlets to produce the same sort of system information. That being the case, I recommend creating a PowerShell script so that you don't have to manually enter a long series of commands each time.

One of the first things that I noticed about the information displayed in Figure 1 is that it is arranged alphabetically. In my opinion, it makes more sense to arrange the information by type. For instance, let's separate the hardware information (CPU and memory) from the network information.

OK, so let's start out by displaying some basic operating system information.  For this section of the output, I am going to display the following information:

  • OS version
  • Install date
  • Service pack version
  • OS architecture
  • Boot device
  • Build number
  • Host name
  • Internet Explorer version

This is actually easier to do than what you might expect. There is a single PowerShell cmdlet that can give us everything except for the Internet Explorer version. That cmdlet is Get-CimInstance. In order to use this cmdlet, we have to append Win32_OperatingSystem and then select the objects that we want to display.  For the sake of the script, I am going to use the following command:

Get-CimInstance Win32_OperatingSystem | Select-Object  Caption, InstallDate, ServicePackMajorVersion, OSArchitecture, BootDevice,  BuildNumber, CSName | FL

You can see the output from this command in Figure 2.

[Click on image for larger view.]  Figure 2. PowerShell can display basic operating system information.

Incidentally, the Get-CimInstance cmdlet can display far more operating system information than what we are using here. If you want to see all of the available operating system information, enter the following command:

Get-CimInstance Win32_OperatingSystem | FL *

You can see what this looks like in Figure 3.

[Click on image for larger view.]  Figure 3. The Get-CimInstance cmdlet can display lots of information.

Displaying the Internet Explorer version number requires a different approach. Rather than using the Get-CimInstance cmdlet, we can use the Get-ItemProperty cmdlet to pull the version number directly from the Windows registry. The command for doing so is:

(Get-ItemProperty 'HKLM:\Software\Microsoft\Internet  Explorer').SvcVersion

You can see what this looks like in Figure 4.

[Click on image for larger view.]  Figure 4. We are displaying the Internet Explorer version number.

These techniques work great except for one thing. The output is a little bit hard to read. That being the case, let's add some text to format our output. You can add text by using the Write-Host cmdlet. The cmdlet displays any text that appears in quotation marks and then automatically moves to the next line. If you want to avoid moving to the next line, you can use the –NoNewLine switch.  I also had to add ForEach{$_.} to the end of each Get-CimInstance command to prevent the column header from being displayed.

Here is a script that displays our operating system information in a way that is easier to read:

CLS
Write-Host -NoNewLine "OS Version: "
Get-CimInstance Win32_OperatingSystem | Select-Object Caption | ForEach{ $_.Caption }
Write-Host ""

Write-Host -NoNewLine "Install Date: "
Get-CimInstance Win32_OperatingSystem | Select-Object InstallDate | ForEach{ $_.InstallDate }
Write-Host ""

Write-Host -NoNewLine "Service Pack Version: "
Get-CimInstance Win32_OperatingSystem | Select-Object ServicePackMajorVersion | ForEach{ $_.ServicePackMajorVersion }
Write-Host ""

Write-Host -NoNewLine "OS Architecture: "
Get-CimInstance Win32_OperatingSystem | Select-Object OSArchitecture | ForEach{ $_.OSArchitecture }
Write-Host ""

Write-Host -NoNewLine "Boot Device: "
Get-CimInstance Win32_OperatingSystem | Select-Object BootDevice | ForEach{ $_.BootDevice }
Write-Host ""

Write-Host -NoNewLine "Build Number: "
Get-CimInstance Win32_OperatingSystem | Select-Object BuildNumber | ForEach{ $_.BuildNumber }
Write-Host ""

Write-Host -NoNewLine "Host Name: "
Get-CimInstance Win32_OperatingSystem | Select-Object CSName | ForEach{ $_.CSName }
Write-Host ""

Write-Host -NoNewLine "Internet Explorer Version: "
(Get-ItemProperty 'HKLM:\Software\Microsoft\Internet Explorer').SvcVersion
Write-Host ""

You can see the command's output in Figure 5.

>
[Click on image for larger view.]  Figure 5. This is the output from the script.

As you can see, it is relatively easy to display system info through PowerShell. Next month I will show you how to add some more information to the output.

About the Author

Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

comments powered by Disqus

Subscribe on YouTube

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025