We are continuing our series that answers common questions, or questions that have been asked about/of PowerShell. Today we talk about Gathering Local System Information. What we intend to provide is one of the many, many ways you can accomplish these same tasks within PowerShell.
All of the solutions are ours and demonstrate the author’s skill and ability level at the time of writing. That is to say, we might not always write the best PowerShell code, however, if you know of a better way we welcome that input.
Let’s start. The question is:
Is there a fast easy way to use Powershell to gather system information?
The Answer:
Yes, it is easy to grab all of your system’s information and output quickly for review. Below is the output from the final script.
To start we use Get-CimInstance
for most of the requests for information from the computer. The easiest command to remember and utilize is:
Get-CimInstance CIM_OperatingSystem
This command will return the System Directory
, that is where Windows is installed on this computer. Build number
, Registered User
, Serial Number
of Windows, and the Version
, which includes the build number.
We do need to know more about this system. For instance, we can gather all of the users that have logged into this computer/server. To do this we use the Get-ChildItem
command. For -Path
we use C:\users\
. The Get-ChildItem
command gets items and child items in one or more specified locations.
Get-ChildItem -Path "C:\Users\"
This command returns all directories under the C:\Users\
directory. We can add this to our script to use later as we gather information from the device. Next we want to get all of the local administrators on this device. Using Get-LocalGroupMember
we can find all members of any local group on the PC. Here is how that would look.
Get-LocalGroupMember -Group "Administrators"
Let’s put
This is all great information for a script to gather and output for us to review later. What else are some more… interesting ways of getting information? I personally like this command:
(Get-CimInstance Win32_OperatingSystem -ComputerName $_.Name).LastBootUpTime
The above command uses Get-CimInstance then uses the class Win32-OperatingSystem telling it to use the $_ inline variable we talk about in:
Below we have taken this idea and expanded upon it. Using multiple one-line commands and storing the data from these commands in variables we created a script that pulls the following information quickly and outputs the data to the screen.