select members from a file object

This topic contains 1 reply, has 2 voices, and was last updated by  Kiran Reddy 3 years, 3 months ago.

  • Author
  • #23748

    Garegin Asatryan

    I know this is a stupid question, but I've spent two hours on this. I know I can use the $_ to select the current object in the pipeline, but I don't know how to select a file object in a given folder and then get it's members. for example I have a file named foo.txt and want to access it's extension property.

    I can do (ls foo.txt).extension, but was wondering if there's another, more direct way.

  • #23750

    Kiran Reddy

    seems you have already found the most direct way 🙂
    if you can access the current object in the pipeline with $_ then that represents the file object but you dont need to use $_ for file objects

    ls | select-object *
    ls | select Name,Extension

    this will get all the file objects in the current directory. If you are looking for a specific file then you can use -filter or where-object

    ls -filter myfile | select *

You must be logged in to reply to this topic.