Insight on Get-ChildItem

Hi All,
sorry for my ignorance. am trying to understand the behavior and haven’t found any thing in MS docs.

Get-ChildItem is behaving differently when supplied with/without Name parameter.

Blockquote
gci *xyz* -Recurse – This is Working
gci -Name *xyz* -Recurse – This is not Working

can someone please provide more insight on why like that?

Thank you!

What do you mean “This is not working”? When I try that it works just as expected.

You may have misundertood something. The paramter -Name is a switch parameter telling Get-ChildItem to return only the names of the found items - not the default porperty set.

You may carefully (re-)read the help for

Thank you @Olaf.

here is the screenshot. I am expecting the same output for both the commands #1, #2.

#1 did not give me any list…however #2 gave me the expected file names.

I am not sure, if I am missing something.

Thank you!

The output would not be the same but at least you should get 2 different lists.

It took me a moment to reproduce the behaviour but I think I got it. It seems that Get-ChildItem ignores the parameter -Recurse when you provide the parameter -Name.

Try the command in a folder where you have files fitting your filter criteria to verify.

You might have found a bug. :man_shrugging:t3:

To circumvent the bug you may simply use

Get-ChildItem -Filter *to* -Recurse | Select-Object -Property Name
2 Likes

Thanks again @Olaf

I did tried in a folder prior to posting the 2nd reply to reproduce(screenshot attached), but the outcome wasn’t consistent with the original command.

It could be a bug . As you mentioned, Get-ChildItem is ignoring the -Recurse when using with “-Name” switch.

Thank you!

I think it’s more complex then bug

The Get-ChildItem cmdlet uses the Path parameter to specify the directory C:\Test. The Name parameter returns only the file or directory names from the specified path. The names returned are relative to the value of the Path parameter.

Name will back only names, to fix is that add path in command, but we have to be aware of what we get back. When name is used we don’t get any more System.IO.FileInfo object.

 Get-ChildItem *get-* -Recurse -name -Path C:\Powershell\*
2 Likes

Adam is correct. See the help file for the -Name parameter on Get-ChildItem:

PS> Get-Help -Name Get-ChildItem -Parameter Name

-Name <System.Management.Automation.SwitchParameter>
    Gets only the names of the items in the location. The
    output is a string object that can be sent down the
    pipeline to other commands. The names returned are
    relative to the value of the Path parameter.

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       False
    Accept wildcard characters?  false

Alternatively, the -Filter parameter is likely what you are looking for.
For example, to search for FILES AND FOLDERS matching “foo” from the root directory, regardless of current working directory, run the following:
Get-ChildItem -Path \ -Filter "*foo*"

Also, you will need to add the -Force parameter to show hidden files.

Cheers and best of luck!