select members from a file object

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Kiran Reddy Kiran Reddy 1 year, 8 months ago.

  • Author
    Posts
  • #23748
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    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
    Profile photo of Kiran Reddy
    Kiran Reddy
    Participant

    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.