list of properties to a Select-Object cmdlet as a variable?

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Vadim Vadim 5 months, 3 weeks ago.

  • Author
    Posts
  • #63516
    Profile photo of Vadim
    Vadim
    Participant

    hi is it possible to pass a list of properties to a Select-Object cmdlet as a variable?

  • #63519
    Profile photo of Sam H
    Sam H
    Participant

    Hi Vadim,

    Do you mean so that you don't have to type them out every time? I'm just starting out on this forum so my answer might not be perfect, but storing the property names in an array then using them in select-object sounds like what you mean? (Let's see how I get on with formatting the code!)

    $Properties = @("Handles","ProcessName","Id")
    Get-Process | Select-Object $Properties
    
  • #63522
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Yes, that is possible. You just need to create an array. The array can contain strings representing the names of the properties, expressions (hashtables) to create calculated properties, or both depending on your use case.

    $propertyList = 'Id', 'Name', 'Threads'
    Get-Process | Select-Object -Property $propertyList
    
  • #63558
    Profile photo of Vadim
    Vadim
    Participant

    thanks, just what i needed

You must be logged in to reply to this topic.