3

Many of you are familiar with the calculated properties possible in PowerShell, to raise a property needed from a sub-object level.

I.e. if I'd like to know the owner of a process (i.e. notepad) as a non-admim I could fetch the process with WMI and query for the owner object using

Get-WmiObject -ClassName Win32_Process -Filter "name='notepad.exe'" | 
  Select-Object -Property ProcessId,Name,@{label='User'; expression={$_.GetOwner().User}}

ProcessId Name        User
--------- ----        ----
    16028 notepad.exe user01
     1972 notepad.exe user01

I could proceed with more properties as Domain

@{l='Domain'; e={$_.GetOwner().Domain}}

ProcessId Name        Domain
--------- ----        ------
    16028 notepad.exe domain01
     1972 notepad.exe domain01

I could also opt to keep the Owner object as is, if I need

@{l='Owner'; e={$_.GetOwner()}}

ProcessId Name        Owner
--------- ----        -----
    16028 notepad.exe System.Management.ManagementBaseObject
     1972 notepad.exe System.Management.ManagementBaseObject

However, how do I go about to keep the object structure while only getting select subobjects?

ProcessId Name        Owner
--------- ----        -----
    16028 notepad.exe @{User=user01; Domain=domain01}
     1972 notepad.exe @{User=user01; Domain=domain01}

1 Answer 1

3

The answer (which is often the case with PowerShell) is really simple. Calculated properties uses a code block {my-code} that can contain any valid commands. So just use Select-Object within the expression.

Get-WmiObject -ClassName Win32_Process -Filter "name='notepad.exe'" | 
  Select ProcessId,Name,@{l='Owner'; e={$_.GetOwner() | Select User,Domain}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.