18

enter image description here

Example I am following:

Get-ChildItem c:\scripts\*.* -include *.txt,*.log

https://technet.microsoft.com/en-us/library/ee176841.aspx

What gives? Why don't I get back a list of my test.txt files when I try to use include?

As a side note, what is c:\scripts\*.*. It seems to be saying include a file with any name that has any format. But isn't that specified in the include? Anyway, more interested in why my seemingly basic code doesn't work.

mklement0
  • 382,024
  • 64
  • 607
  • 775
VSO
  • 11,546
  • 25
  • 99
  • 187

2 Answers2

30

From the help file (Get-Help Get-ChildItem):

The Include parameter is effective only when either the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows*, where the wildcard character specifies the contents of the C:\Windows directory.

Get-ChildItem c:\pstest\*.* -include *.txt

or

Get-ChildItem c:\pstest -recurse -include *.txt

or better yet: use the -Filter parameter instead of -Include:

Get-ChildItem C:\pstest -Filter *.txt
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thank you, both of those work, but I am not following why I need " \*.\* " when I am already looking for filtered files in `pstest` (or whatever dir), seems redundant (though I am sure I am missing something). – VSO Jan 17 '17 at 15:22
  • 1
    @VSO in that case you should be using `-Filter`, not `-Include` (see update) – Mathias R. Jessen Jan 17 '17 at 16:15
  • Alright, thanks, I will look at the differences between the two. – VSO Jan 17 '17 at 16:26
  • 1
    This is completely broken. Literally calling Get-ChildItem on '*' (i.e. the current directory) and the -Include pattern is not matched. Worse, the -Exclude pattern is not matched either. It just doesn't work. Using -Recurse doesn't work either, nor does it even make sense to use it, since I'm not trying to recurse... literally just trying to Get-ChildItem(s) of the current directory. -Filter is not a workaround, as I need to Include one pattern while simultaneously excluding another. It's just non-functional. – Triynko Sep 24 '21 at 00:02
  • 2
    "The implementation of -Include / -Exclude with Get-ChildItem is unintuitive and has pitfalls". No kidding. https://stackoverflow.com/a/38308796/88409 – Triynko Sep 24 '21 at 00:06
2

you can do it simply too:

Get-ChildItem "C:\pstest\*.txt"

If you want to create more elaborate filters:

Get-ChildItem "C:\pstest\" -recurse -file | where {$_.Name -like "*hello*" -and $_.Name -notlike "*good by*"}
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • -Include requires the trailing wildcard. If you want to do this and be OS-independent then you'd need to use join-path `$testdirectory = "C:\some\path\with\media\files" get-childitem -path ( join-path $testdirectory *) -Include *.mp4,*.mkv,m4v,m4a` That safely puts the delimiter on the of the path so -Include will do it's thing properly. – wkearney99 Jan 15 '23 at 19:08
  • I hate markdown, sorry for the repeated edits and failure to just put some damned linebreaks in there. – wkearney99 Jan 15 '23 at 19:13