# win32\_computersystem and win32\_bios in 1 command

**URL:** <https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351>\
**Category:** PowerShell Help\
**Created:** [June 11, 2015, 8:28pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351 "2015-06-11T20:28:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt-saunders](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@matt-saunders](https://forums.powershell.org/u/matt-saunders)\
**Post date:** [June 11, 2015, 8:28pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/1 "2015-06-11T20:28:34Z")

</div>

Hey guys

I am very new to PowerShell, I need to get information about our PCs at work

Manufacturer  
Model  
Name  
Serial

I have used this and it works but I do not get the serial

```
Invoke-Command -ComputerName 10.100.2.77,10.100.2.83 -Credential domain\administrator { Get-WMIObject Win32_Computersystem } | Format-Table Name,Manufacturer,Model
```

How I know I can get this information from win32\_computersystem and from win32\_bios

The problem I am having it getting all the information in one command, so far I have created two variables

```
$ComputerSystem = Get-WmiObject win32_computersystem 
$bios = Get-WmiObject win32_bios
```

Now I think I need to create a foreach loop to put all this together, any help would be fantastic

Thanks Guys

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [June 11, 2015, 10:08pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/2 "2015-06-11T22:08:03Z")

</div>

This is quite easy to do with the following function (and I’m assuming you have administrative access to those computers):

```
#requires -Version 1
Function Get-ComputerDetails
{
    [cmdletbinding()]
    Param([string[]]$Computer)
    
    $result = @()

    foreach ($node in $Computer)
    {
        try
        {
            $systemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $node -ErrorAction Stop
            $systemBios = Get-WmiObject -Class Win32_Bios -ComputerName $node -ErrorAction Stop
            
            $customObject = [pscustomobject][ordered]@{
                Name = $systemInfo.Name
                Manufacturer = $systemInfo.Manufacturer
                Model = $systemInfo.Model
                Serial = $systemBios.SerialNumber
            }
        }
        catch 
        {
            Write-Error -Message "The command failed for computer $node. Message: $_.Exception.Message"
            break
        }
        $result += $customObject
    }
    $result
}
```

You use this function as following:

```
Get-ComputerDetails -Computer or01,dc01,ipam01
```

If you want to output to a csv file, just as easy:

```
Get-ComputerDetails -Computer or01,dc01,ipam01 | Export-Csv -Path 'C:\Temp\Export.csv' -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![matt-saunders](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@matt-saunders](https://forums.powershell.org/u/matt-saunders)\
**Post date:** [June 11, 2015, 10:23pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/3 "2015-06-11T22:23:35Z")

</div>

Thank you so much for the reply

I think I may be doing something wrong since I don’t get any output from the command, this is what I have done

Copied the code and saved it as Get-ComputerDetails.ps1

ran the following command

```
.\Get-ComputerDetails.ps1 -computer localhost | Export-Csv C:\Users\admin\Desktop\Details.csv
```

The file is empty

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [June 11, 2015, 10:31pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/4 "2015-06-11T22:31:31Z")

</div>

It’s a function. So in order to use it you either have to load it in your PowerShell User profile, or creating a new script, and dot source the Get-ComputerDetails.ps1 file.

To use it immediately, try to open up PowerShell ISE, and paste the function in the script pane. After that select all the code and then press F8. The function is now loaded in memory, and you can use it in the current PowerShell session (the console below the scriptpane).

If you want to use it in all your PowerShell sessions, put the function in your PowerShell profile. Remember, PowerShell.exe and PowerShell\_ise.exe use different profiles.

The profiles exists in this path: %username%\My Documents\WindowsPowerShell\

If you want to dot source it in a script, you have to do this:

. C:\Get-ComputerDetails.ps

Get-ComputerDetails -computer localhost

---

<div class="post-metadata">

**Author:** ![matt-saunders](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@matt-saunders](https://forums.powershell.org/u/matt-saunders)\
**Post date:** [June 11, 2015, 11:11pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/5 "2015-06-11T23:11:55Z")

</div>

Thank you so much, it is working, is it possible to add the name of the user who last logged onto the PC?

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 11, 2015, 11:43pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/6 "2015-06-11T23:43:19Z")

</div>

Windows doesn’t specifically track the last logged-on user. On a server computer, people are authenticating constantly to file shares and other resources, so the OS in general doesn’t keep track. If you’ve enabled security auditing, you could query the most recent event log entry for logons.

I’ll note, with bias, that a lot of what you’re trying to do with functions and such is very thoroughly covered in “Learn PowerShell Toolmaking in a Month of Lunches.” That might be an easier way to learn some of this stuff without feeling like you’re banging your head into a wall :).

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [June 11, 2015, 11:44pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/7 "2015-06-11T23:44:29Z")

</div>

In a way, yes. But it’s not really trustable. As long as you didn’t specify in your GPO’s that the last logged on user should not be cleared in the login box (the one you see when you need to login into the computer), then this addition is more or less acceptable:

```
#requires -Version 1
Function Get-ComputerDetails
{
    [cmdletbinding()]
    Param([string[]]$Computer)
    
    $result = @()

    foreach ($node in $Computer)
    {
        try
        {
            $systemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $node -ErrorAction Stop
            $systemBios = Get-WmiObject -Class Win32_Bios -ComputerName $node -ErrorAction Stop
            
            $customObject = [pscustomobject][ordered]@{
                Name = $systemInfo.Name
                Manufacturer = $systemInfo.Manufacturer
                Model = $systemInfo.Model
                Serial = $systemBios.SerialNumber
                LastLoggedOn = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI' -Name 'LastLoggedOnSAMUser' | Select-Object -ExpandProperty LastLoggedOnSAMUser
            }
        }
        catch 
        {
            Write-Error -Message "The command failed for computer $node. Message: $_.Exception.Message"
            break
        }
        $result += $customObject
    }
    $result
}
```

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [June 11, 2015, 11:47pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/8 "2015-06-11T23:47:22Z")

</div>

And Don is right.

I’m throwing this functions to you assuming you understand.

But follow Don’s advice and read that book. (-:

And to be honest, I’m also learning how to create proper functions.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [June 12, 2015, 5:38am UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/9 "2015-06-12T05:38:10Z")

</div>

There are many ways to do the same thing, your end goal is to create a single object with all properties. I see that system info has the most properties, so I just use Select-Object to get those properties and use calculated properties to cover the last 3 properties. I try to use this method first and if there is a need to order the results or it’s a bunch of single properties from separate sources, then I would use New-Object or a similar method. Here is the excerpt:

```
$node = "."

$systemBios = Get-WmiObject -Class Win32_Bios -ComputerName $node -ErrorAction Stop
Get-WmiObject -Class Win32_ComputerSystem -ComputerName $node -ErrorAction Stop | 
Select Name,
       Manufacturer,
       Model,
       @{Label="Serial";Expression={$systemBios.SerialNumber}},
       @{Label="LastLoggedOn";Expression={Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" -Name "LastLoggedOnSAMUser" | Select-Object -ExpandProperty LastLoggedOnSAMUser}}
```

---

<div class="post-metadata">

**Author:** ![matt-saunders](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@matt-saunders](https://forums.powershell.org/u/matt-saunders)\
**Post date:** [June 12, 2015, 5:09pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/10 "2015-06-12T17:09:50Z")

</div>

Guys you have been very helpful, it’s great to find a PowerShell community where I can get help so quickly 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/win32-computersystem-and-win32-bios-in-1-command/4351/11 "2024-05-16T20:45:08Z")

</div>


