using select-object

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 4 months ago.

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

    Hello

    let's say I run get-childitem

    Name
    —-
    AdwCleaner
    cygwin64
    DigiNet_Center
    Drivers
    FRST

    I want to select an item by name like choosing drivers or cygwin64. How can I do that? I know I can select the first 3 or last 2 with the appropriate switches, but don't know how I do it with the name of the object.

    Thanks

  • #17803
    Profile photo of Don Jones
    Don Jones
    Keymaster

    BY using Where-Object, and a property name like Name.

     ... | Where { $_.Name -like 'cyg*' }

    Or, use the -Filter, -Include, or -Exclude parameters of Get-ChildItem.

  • #17804
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    what does ... mean? probably a typo.
    I guess my real question is whether I can extract the name property out of the object and "select it".

    If I do

    ls | ? name -like Ad* | ft name

    I only get the name property, but not the name by itself. If I pipe that into another command like mkdir, it doesn't work. How do I sanitize the result so that it is good to go for another command.

    Or to make it even simpler

    ls -name Ad*

    works fine. But ls -name Ad* | mkdir fails.

  • #17806
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    Hope this helps.

    First part of what you are doing is you want to select the name done below.

    instead of using ls, you could also use get-childitem

    [b]get-childitem | where {$_.name -like 'Te*' } | Select Name[/b]

    Instead of using ft, which is format table do another select. Although it appears the result is the same it isn't. If you was to pipe your command again using Get-Member (GM) you would see that it is not of type 'FormatStartData' whereas using select is type 'FileInfo' . This does make a difference when you are now tring to pipe it to something else.

    So now when you do a Get-Member using Select you will see that you have a 'Name' property which you can select so I have just put the first part into a variable – you don't have to, just one way to do it i guess.

    [b]$name = get-childitem | where {$_.name -like 'Te*' } | Select Name[/b]

    [b]$name.name[/b] – This as you are referring to is 'selecting' the actual name, if you did get a Get-Member on this, you would notice it to be of type String

    The next part is mkdir, which is also New-Item however checking the help file on New-Item it seems to take the name property as string or if you want to pipe to it then its by value so we can't use $name.name, as that is a string

    [b]$name | New-Item -ItemType directory [/b]

    This will error and its nothing to do with Powershell. It has to do with Windows. You cannot have a file and folder with exactly the same name in the same folder i.e File: text.txt and Folder: test.txt – That wont work, however you can force it to override the file by doing a -force

    [b]$name | New-Item -ItemType directory -Force [/b]

    This will work but it will just override the text file. If you was to delete that file and run the command again you would find that it created it as you wanted and as a folder instead.

    Hopefully some of that made sense.

    Thanks

  • #17807
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I think what you're describing is the behavior of Select-Object -ExpandProperty. When you use this parameter, the output will be the objects that are stored in that property of the input objects. If you use the -Property parameter instead, what you get are PSObjects that contain the property you specified. For example:

    # This produces a collection of PSObjects with a Name property.  This Name property will be a reference to a String object, in this case.
    Get-ChildItem | Select-Object Name
    
    # This produces a collection of strings:
    Get-ChildItem | Select-Object -ExpandProperty Name
    

You must be logged in to reply to this topic.