July 4, 2025

USEFUL-IT

A blog for USEFUL-IT information

Construct your own Custom PSObject

How to create your own Custom PSObjects.

Sometimes it is useful when curtain data is combined in one PSObject.
For instance we want to know the owner of the processes running on a machine
I am going to explain how to do this.

First we define an empty object

$PSobject=@()

The second thing we are going to do is fetch some data to put into the Object.

foreach ($process in (Get-WmiObject -Class win32_process)){
    $owner = $process.getowner().user         #Set the owner in a variable
    if ($owner -like ""){$owner = 'SYSTEM'}   #If the variable is empty we put 'System' in

    $datatable = @{
        User = $owner
        Handles = $process.handles
        name = $process.name
    }
}

After the data is fetched we add the data to the object.

foreach ($process in (Get-WmiObject -Class win32_process)){
    $owner = $process.getowner().user         #Set the owner in a variable
    if ($owner -like ""){$owner = 'SYSTEM'}   #If the variable is empty we put 'System' in

    $datatable = @{
        User = $owner
        Handles = $process.handles
        name = $process.name
    }
    $PSobject += New-Object psobject -Property $datatable
}

And then we output the PSObject to the screen

$PSobject

Result :

name                         User      Handles
----                         ----      -------
System Idle Process          SYSTEM          0
System                       SYSTEM       1397
smss.exe                     SYSTEM         49
csrss.exe                    SYSTEM        388
...
...
...
sihost.exe                   Useful-IT     427
taskhostw.exe                Useful-IT     334
explorer.exe                 Useful-IT    1656
RuntimeBroker.exe            Useful-IT     613
ShellExperienceHost.exe      Useful-IT     685
SearchUI.exe                 Useful-IT     836
SettingSyncHost.exe          Useful-IT     153
HkeyTray.exe                 Useful-IT     409
svchost.exe                  Useful-IT     174
ApplicationFrameHost.exe     Useful-IT     200
ssh-agent.exe                Useful-IT      95
powershell_ise.exe           Useful-IT    1101
conhost.exe                  Useful-IT     113
powershell.exe               Useful-IT     701
conhost.exe                  Useful-IT     129

Full script :

$PSobject=@()
foreach ($process in (Get-WmiObject -Class win32_process)){
    $owner = $process.getowner().user         #Set the owner in a variable
    if ($owner -like ""){$owner = 'SYSTEM'}   #If the variable is empty we put 'System' in

    $datatable = @{
        User = $owner
        Handles = $process.handles
        name = $process.name
    }
    $PSobject += New-Object psobject -Property $datatable
}

$PSobject

To make this ready for a module/function use the following :

Function :

Function Get-UserProcesses {
    foreach ($process in (Get-WmiObject -Class win32_process)){
        $owner = $process.getowner().user         #Set the owner in a variable
        if ($owner -like ""){$owner = 'SYSTEM'}   #If the variable is empty we put 'System' in
        $datatable = @{
            User = $owner
            Handles = $process.handles
            name = $process.name
            }
        New-Object psobject -Property $datatable
    }
}

Get-UserProcesses

About The Author