Determine Hardware Type โ€“ 2012 PowerShell Scripting Games Beginner Event #8

Write a script to determine if a computer is a laptop or a desktop from a hardware prospective and display the output on the console. If this requires admin rights, you should detect if it is running as an admin or standard user. Extra points for writing a simple function that returns a boolean.

I kind of figured this was going to be a WMI thing since it involved hardware. Some research turned up an MSDN article on the Win32_ComputerSystem class which contained a PCSystemType property. You could also search through the WMI classes using Get-WMIObject -List \*computer\*.


Based on that MSDN article, only a value of 2 is a mobile computer (laptop). Based on some of the comments listed on the scenario requirements for this event, it appears to be safe to assume that all others are considered to be desktops.


I broke out my Learn Windows PowerShell in a Month of Lunches book and headed to chapter 19 to figure out how to write a simple function. Section 19.5 - Returning objects from a function was close enough to figure it out. I used If/Else and designed it so Else would never have to run unless the value was equal to 2.


Then all I had to do is call the function and display the output based on whether or not the function returned true or false with another If/Else statement.

function Get-HardwareType {


Get-HardwareType is used to determine if a computer is a laptop of desktop.

Get-HardwareType is used to determine a computer's hardware type of whether or not the
computer is a laptop or a desktop.


    $hardwaretype = Get-WmiObject -Class Win32_ComputerSystem -Property PCSystemType
        If ($hardwaretype -ne 2)
        return $true
        return $false

If (Get-HardwareType)
"$Env:ComputerName is a Desktop"
"$Env:ComputerName is a Laptop"


After I wrote this blog, but before it was published I received some peer feedback on this event (which I appreciate). Based on this feedback, it appears that I’m returning the entire object instead of the single element (PCSystemType) as shown here:


This makes the function always return true.

The code in my script could have been written like this by expanding the property:


Here’s the complete script using this method (option #1):


Or by using the dotted notation (option #2):


Here’s the complete script using that method:


Or like this using the dotted notation in another way (option #3):


I’m glad this error was caught since I didn’t realize there was an issue with the script I submitted. This allows me to learn from it and write better code in the future because the scripting games aren’t about winning or losing, they’re about learning. I want to remind the judges that this is why it’s so important to leave feedback. A three star rating with no comments means absolutely nothing!