using select-object


let’s say I run get-childitem



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.


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.

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.

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

get-childitem | where {$ -like ‘Te*’ } | Select Name

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.

$name = get-childitem | where {$ -like ‘Te*’ } | Select Name

$ - 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 $, as that is a string

$name | New-Item -ItemType directory

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

$name | New-Item -ItemType directory -Force

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.


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