Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `select` to an array of strings

Tags:

powershell

When I use the Select-Object Name CmdLet it seems to create a new object with a single Name property on it.

I often want to pipe this selection to other CmdLets but they often take just a string.

How can I easily get a bunch of objects and say "Select only property x and just the property values into an array or collection of just its values"?

like image 792
Luke Puplett Avatar asked Mar 09 '23 07:03

Luke Puplett


1 Answers

You can use the ExpandProperty parameter for this. This switch means that instead of returning an object with properties as listed on the (default) -Properties parameter, the value of the single property listed under -ExpandProperty parameter is returned.

NB: You can also use the alias, expand for this parameter.

Example:

Get-Process | Select-Object -ExpandProperty ProcessName

Related documentation:

  • SS64: https://ss64.com/ps/select-object.html
  • MS Docs: https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Select-Object?view=powershell-3.0
like image 120
JohnLBevan Avatar answered Mar 15 '23 19:03

JohnLBevan