Searching for Multiple File Types in PowerShell

This is a quick tip for people that would like to search for multiple file types in Windows using PowerShell.

To begin, let’s look at what you would need to do using the Windows Command Shell. If you wanted to see all the files in a directory that are of type .json. You could do something like this:

> dir *.json

Volume in drive C is OSDisk
Volume Serial Number is A415-C42C

Directory of C:\temp

12/10/2019 06:47 AM 174 db.json
12/02/2019 06:36 AM 159,907 package-lock.json
12/02/2019 06:36 AM 803 package.json
3 File(s) 160,884 bytes
0 Dir(s) 54,596,169,728 bytes free

But instead, what if you wanted to look for all .json files AND .yml at the same time? Back to the shell, you could type this:

> dir *.json *.yml

Volume in drive C is OSDisk
Volume Serial Number is A415-C42C

Directory of C:\temp

12/10/2019 08:47 AM 174 db.json
12/02/2019 07:36 AM 159,907 package-lock.json
12/02/2019 07:36 AM 803 package.json

Directory of C:\temp

12/06/2019 10:31 AM 3,072 _config-example.yml
12/06/2019 10:18 AM 3,085 _config.yml
5 File(s) 167,041 bytes
0 Dir(s) 54,595,665,920 bytes free

That works but the Command Shell isn’t the easiest to work with if you then wanted to process those files in other ways, programmatically. Let’s jump to PowerShell which is significantly better in that regard. Starting with the first example, we could use Get-ChildItem.

> Get-ChildItem -Path .\*.json

Directory: C:\temp

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/10/2019 6:47 AM 174 db.json
-a---- 12/2/2019 6:36 AM 159907 package-lock.json
-a---- 12/2/2019 6:36 AM 803 package.json

Here, we’re asking PowerShell to show us all files in the current directory (.\) which have the extension .json. That’s fine, but not a huge improvement on the Command Shell. OK - what if we are interested in two types of file extension? Using the same technique as in the Command Shell, just won’t work.

> Get-ChildItem -Path .\*.json *.yml

--No Output--

The trick here is to refer to the documentation. Here’s the docs for the command I mean.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Get-ChildItem
[[-Path] <string[]>]
[[-Filter] <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Recurse]
[-Depth <uint32>]
[-Force]
[-Name]
[-Attributes <FlagsExpression[FileAttributes]>]
[-FollowSymlink]
[-Directory]
[-File]
[-Hidden]
[-ReadOnly]
[-System]
[<CommonParameters>]

The bits we need to focus on are lines 2 and 4: -Path and -Include.

The -Path is used to specify the location and set of ALL files which could be used in the listing of files. So, we could for instance ask for all files in the current directory like this: -Path .\*. What about the -Include parameter, then?

That’s used to tell PowerShell which files, from all of the files, we would like to include. So in this case, we’re after the .json and .yml files. Before I do that though, let’s take a closer look at line 4 and the syntax used in the MS help page.

[-Include <string[]>]

The outer square brackets tell us that this command line parameter is optional, but look closer, and you can see that there are two more square brackets attached to the string segment. In this case, it’s telling us that the parameter can accept an array of strings. Here’s our final example demonstrating that using the same files as before.

> Get-ChildItem -Path .\* -Include "*.json","*.yml"

Directory: C:\temp\sm\github\logicalmoon.com

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/10/2019 6:52 PM 4363043 db.json
-a---- 12/2/2019 6:36 AM 159907 package-lock.json
-a---- 12/2/2019 6:36 AM 803 package.json
-a---- 12/6/2019 6:31 AM 3072 _config-example.yml
-a---- 12/6/2019 6:18 AM 3085 _config.yml

As you can see, to specify the array of strings, all we have to do is separate them with a comma and now that we are using PowerShell, we can pipe that output to another command or store the set of files as objects in a variable with something like this:

> $results = Get-ChildItem -Path .\* -Include "*.json","*.yml"

Do take a look at the manual page for Get-ChildItem as there’s lots more you can do with this command.


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com