1

If I run the following, I get the following results.

[string[]]$p = Get-Process Explorer |Select-Object -Property ID,Name,Handles,MainWindowTitle
$p

foreach($item in $p)
{
    Write-Output $item
}


@{Id=3560; Name=explorer; Handles=1454; MainWindowTitle=C:\}
@{Id=3868; Name=explorer; Handles=2787; MainWindowTitle=Y:\}
@{Id=9468; Name=explorer; Handles=1823; MainWindowTitle=Z:\}

Question 01: Is this considered to be a hashtable?

Question 02: How can I get the values of a key such as MainWindowTitle?

Question 03: How would I filter on ID field ending with 68?

  • 1
    {i) An array of strings (```[string[]]```) is not a ```hashtable```. Note that ```Get-Process``` will return a ```PSCustomObject``` or a ```System.Object[]``` depending on how many matching results there are. (ii) ```foreach( $item in (Get-Process Explorer) ) { write-output $item.MainWindowTitle }``` (iii) Use the modulus operator - ```Get-Process | where-object { $item.Id % 100 -eq 68 }``` – mclayton Aug 21 '22 at 22:01
  • Select-Object returns PSObject See https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-5.1 – Carsten.R Aug 21 '22 at 22:03
  • @mclayton, good pointers, but note that `Get-Process` situationally returns `[System.Diagnostics.Process]` or `[System.Diagnostics.Process[]]` – mklement0 Aug 21 '22 at 22:15
  • @Carsten.R, while the docs state `[PSObject]`, which is _technically_ correct, it's more helpful to say that `Select-Object` returns `[pscustomobject]` instances, given that's the type accelerator you need to use to create your own instances - see [GitHub issue #](https://github.com/PowerShell/PowerShell/issues/4344) for a discussion. – mklement0 Aug 21 '22 at 22:19
  • 1
    @mklement0 - ah yeah... ```Get-Process ... | Select-Object ...``` returns ```PSCustomObject``` or ```Object[]```, not the bare ```Get-Process```. – mclayton Aug 21 '22 at 22:24

1 Answers1

1

Question 01: Is this considered to be a hashtable?

No: you're explicitly creating a string array ([string[]]), which means that the [pscustomobject] instances emitted by Select-Object, based on the [System.Diagnostics.Process] instances that Get-Process outputs, are stringified.

A stringified [pscustomobject] uses a hashtable-like representation that is unrelated to actual hashtables - see this answer.

Note:

  • This is a for-display, string representation that is not suitable for programmatic processing, notably because information about the data types of properties and boundaries around string property values with spaces are lost.

  • For that reason, in general, avoid dealing with string representations of objects in favor of processing objects as-is, which enables robust, property-based access.


Question 02: How can I get the values of a key such as MainWindowTitle?

Use the [System.Diagnostics.Process] objects output by Get-Process as-is, which allows you to access their properties (no need for an intermediate Select-Object call); the following example uses member-access enumeration to retrieve all .MainWindowTitle property values:

(Get-Process Explorer).MainWindowTitle

Question 03: How would I filter on ID field ending with 68?

Building on the above, using Where-Object with simplified syntax for filtering:

Get-Process Explorer | Where-Object ID -like *68
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thank You for your explanation and reply. Question 2 & 3: I know how to access and filter the field with those methods but was hoping to understand how to access the field when it is in this format: @{Id=3560; Name=explorer; Handles=1454; MainWindowTitle=C:\} @{Id=3868; Name=explorer; Handles=2787; MainWindowTitle=Y:\} @{Id=9468; Name=explorer; Handles=1823; MainWindowTitle=Z:\} – LiquidMetal Aug 21 '22 at 22:23
  • @LiquidMetal, there are no _fields_ in this _string_ representation, which is purely a _for-display_ representation that is _not_ suited to programmatic processing. I've also updated the answer to that effect. – mklement0 Aug 21 '22 at 23:00
  • Thank You MkleMent0. I am new to the forum, actually rarely use this forum. Is there a way to thank you for the answer like clicking on a "thank you" icon? Regardless, thank you again for the explanation and the answer. I am also new to PowerShell. I am converting all of my VBScript over. I am amazed with some features in Powershell only required a few lines of coding to do the same thing as my VBScript such as recursive files. – LiquidMetal Aug 22 '22 at 01:18
  • @LiquidMetal, yes, while learning PowerShell takes time, it's definitely worth the effort, as it allows you to be much more productive with much fewer lines of code in the long run. As for thanking me: that is usually done indirectly, by [accepting](https://meta.stackexchange.com/a/5235/248777) an answer, which not only rewards the answerer with reputation points, but - more importantly overall - also signals to future readers which answer solved your problem. With 15 reputation points or more (which you already have), you can additionally _up-vote_ answers, on _any_ question, not just yours. – mklement0 Aug 22 '22 at 01:35