{gci -file} – PowerShell Lists all files in Directory 2020

In Powershell list all files in a directory is a very easy task.  We need to only use the following command to just list all the files only in a directory.

Table of contents:

  • PowerShell List files in a Directory.
  • gci alias for get-chiltitem.
  • dir alias for get-childitem.
  • PowerShell list only directories.
  • PowerShell list only directories using a shorter script.
  • PowerShell list files recursively.
  • PowerShell list directories recursively.
  • List files of a certain extension type.
  • PowerShell list files of a certain extension type recursively.
  • List files to a depth in subdirectories.
  • Verdict.

People also search for:

[Solved] Working Snippet for Start-Job PowerShell 2020

gci -file

The above script is shown is executed and the output is shown in the image below.

get all the files in a directory
get all the files in a directory

There are many variations to it and we visit quite a few of them in this post.  We will see how the gci alias originates and we will also see some different alias also.  There are ways to get only the directories within directories and also up to certain depth.

We will visit as many features as we can about listing files in a directory.

PowerShell List Files in a Directory:

PowerShell developers made it unjustifiably easy to deal with files and folders in PowerShell.  Unlike traditional scripting, we do not have to think too much about structuring the script in PowerShell.  That is because PowerShell is object-based input and output.  If anyone is good at an object-oriented programming language, scripting on PowerShell comes naturally to them.

To begin with, getting only the files in a directory, we can use the get-childitem cmdlet. We will add the -file switch to the cmdlet to fetch only the files in a specific directory.

get-childitem -file

get child item file
get child item file

Clearly this solves our problem.  But can we make the script more efficient?  In fact, we can.

gci Alias for Get-ChildItem:

The get-childitem cmdlet has an alias called gci.  It decreases the effort to write the whole script.  The below script will produce the output we need.

gci -file

gci -file
gci -file

dir alias for Get-ChildItem:

The get-childitem has a rather popular alias also.  It can be replaced with dir to get the same results.

dir -file

The above script will produce the output as follows.

dir file
dir file

PowerShell list only directories:

We have learned how to list only files in a specific directory.  Now we will see how to list only the directories from a directory.  We can do that with the help of -directory switch instead of the -file switch.

gci -directory

gci -directory
gci -directory

PowerShell list only directories shorter script:

We can simplify the script with the help of -di switch.  It is available as it is enough to identify the -directory switch without any ambiguity.  Use the following script to list only the directories in the directory.

gci -di

This will give users the following output.

gci -di
gci -di

PowerShell list only files in all the folders or directories:

Now that we have seen how to list only the files or only the folders, we still have not learned how to list only the files in the directory.  We can do just this with the help of -recurse switch.  The script is given below.

gci -file -recurse

get files recursively
get files recursively

Clearly this is a great script to get to only the files in the directory or the folder.

PowerShell list only folders in all the folders or directories:

We can list only the files in the directory and also the subsequent child directory using the following script.

gci -di -recurse

recursive directories
recursive directories

PowerShell list files of an extension:

We can list only a certain type of files in a directory if we wish.  We can list only PDF files in a directory using the following script.

gci -file *.pdf

only the pdfs
only the pdfs

PowerShell list files of an extension recursively:

We can take the listing of the files to a new level by listing only the files of an extension recursively.  The below example demonstrates just that.

gci -file -r *.pdf

file type recursively
file type recursively

Note how we use -r switch instead of the -recurse switch as it decreases the amount of typing that needs to be done.

PowerShell list files of an extension recursively with depth:

We can list all the files in a directory and subsequent subdirectory using the -recurse switch.  We can also narrow down the search using the file extension.  But what if we do not want to search in folders that are deeper than 2 subdirectories.  We can use the -depth switch.  The below script uses that.

gci -file -d 1 -r *.pdf

with depth
with depth

The above script will get-childitem of type files, it will fetch up to child directories 1, It will do it recursively and finally, it will ensure that it will fetch only pdfs.

Verdict:

In this post, we learned about PowerShell List all files in a Directory.  We did this using the get-childitem alias called gci.  The gci coupled with the -file switch does what we need it to do.  We also fetched the files of a certain extension also.  We got the files of a certain extension recursively and also up to a limited depth.

If you want more awesome posts, stay tuned to TecKangaroo.

Leave a Comment