Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-ChildItem Doesn't Work With Include

Tags:

powershell

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.

like image 904
VSO Avatar asked Jan 17 '17 15:01

VSO


People also ask

How do I Get-ChildItem to exclude folders?

To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter. Gets files.

How do I Get-ChildItem in PowerShell?

The Get-ChildItem cmdlet uses the Path parameter to specify the directory C:\Test . Get-ChildItem displays the files and directories in the PowerShell console. By default Get-ChildItem lists the mode (Attributes), LastWriteTime, file size (Length), and the Name of the item.

How do you get the full Path of ChildItem?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.

How do I get a list of files in a directory in PowerShell?

Like the Windows command line, Windows PowerShell can use the dir command to list files in the current directory. PowerShell can also use the ls and gci commands to list files in a different format.


1 Answers

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
like image 198
Mathias R. Jessen Avatar answered Sep 22 '22 15:09

Mathias R. Jessen