Powershell

List Files in a Directory with PowerShell


PowerShell is a freely available, open-source Microsoft automation and scripting platform. Its goal is to assist its users in creating efficient tools and scripts for helping them accomplish their jobs more effectively. Many useful functions and commands are available in PowerShell, which are known as cmdlets. These cmdlets are utilized to perform a specific operation, such as pinging a remote computer, reading a particular file, etc.

When you want to know what files are on your system, you surely need to list them. Many users may believe that exploring files in the Graphical User Interface (GUI) is far more convenient than messing with the command line or PowerShell. But that’s not the truth. Listing files is an operation that PowerShell does effortlessly. If you have any problem listing files in a directory with PowerShell, then this post is here to rescue you! PowerShell utilizes the “Get-ChildItem” command to list files present in a directory. Let’s know more about this commendable command.

What is Get-ChildItem?

In PowerShell, “Get-ChildItem” performs the same function as “dir” in the Windows command prompt. This cmdlet retrieves data from a specific location. It also lists out objects or items from one or more file locations provided. The items will get their child items from the container. Registry and Files are referred to as child items in the subfolders of PowerShell. Like the “dir /s,” if you want to retrieve data from the child containers, you must use the “-Recurse” parameter.

A file system can be anything from a certificate store to a registry hive shared path directory or local. When you utilize “Get-ChildItem” on a system, it lists files, directories, and subdirectories. In the other case, when you use it on a directory, this command returns a list of files and subdirectories that comes under it. Get-ChildItem does not show empty directories when “-Recurse” or “-Depth” options are used in a Get-ChildItem command.

Operators used with Get-ChildItem command in PowerShell

Following are the operators that are used with the Get-ChildItem cmdlet:

  • ,” for OR
  • +” for AND
  • !” for NOT

Attributes of Get-ChildItem command in PowerShell

Here are some of the attributes of the Get-ChildItem cmdlet, which are utilized to list files or folders according to specified attributes.

  • link (l)
  • system (s)
  • directory (d)
  • hidden (h)
  • read-only (r)
  • archive (a)

Let’s check some examples of listing files in PowerShell.

Example 1: Listing child items in a specific directory using the “-Path” parameter

If you want to list files and directories of a specific directory, utilize the “-Path” parameter in the “Get-ChildItem” command. This option will help PowerShell list all the child items of the specified directory. The “-Path” parameter is also utilized to set the paths of one or more locations of files. If you do not explicitly mention the directory path, the current working directory will be the default location.

In the below-given example, the PowerShell will list all child files and folders present in the “E:\vbox\” directory:

> Get-ChildItem -Path E:\vbox\

If you do not add the “-Path” parameter, the “Get-ChildItem” cmdlet will take the first parameter as the directory path. Execute of this command will show you the same output:

> Get-ChildItem E:\vbox\

Example 2: Listing child items and their subdirectories using the “-Recurse” parameter

The “-Recurse” is the parameter that searches for the files and folders in the subdirectories of the specified path. If you want to store all this information, then utilize a PowerShell container, save the details such as Name, Length, and Full Name of Child items. After that, the “Get-ChildItem” command will retrieve all of this information from the container and list the files, directories, and the child items’ subdirectories.

> Get-ChildItem -Recurse "E:\software" | Where { ! $_.PSIsContainer } | Select Name,FullName,Length

You can retrieve the information as per your requirements. Here, we have excluded the “Length” of the files and directories:

> Get-ChildItem -Recurse "E:\vbox" | Where { $_.PSIsContainer } | Select Name,FullName

Example 3: Listing files in a directory using the “-Exclude” parameter

The “-Exclude” is a string parameter that excludes directories and files with some specific extension. It is specified after adding the path of the directory. Wildcard characters are also utilized for this purpose, such as *.txt is used in the below-given example:

> Get-ChildItem -Recurse "E:\UWT4" -Exclude *.txt | Where {! $_.PSIsContainer } | Select Name,FullName

The execution of this command will list out all the directories and files except for the files having the “.txt” extension.

Example 4: Listing files in a directory using the “-Include” parameter

The “-Include” is a string parameter utilized in the “Get-ChildItem” cmdlet to list specific files and folders. You can specify more than one extension with the “-Include” option, separated by a comma. For example, we will include all files having “.txt” extension in the “C:\Windows\System32” directory:

> Get-ChildItem -Path C:\Windows\System32\* -Include *.txt

Example 5: Listing files in a directory using the “-Descending” parameter

While listing out the files in PowerShell, you can sort them out based on various attributes such as the files’ name or the Length of the files.

In the below-given command, the “Get-ChildItem” will print out the list of files and directories sorted descending by their Length:

> Get-ChildItem -Path E:\UWT4 -Recurse -File | sort length -Descending

Example 6: Listing files in a directory using the “-Depth” parameter

If you want to control the recursion of the directories, then use the “-Depth” parameter in your “Get-ChildItem” command. By default, when you execute the “Get-ChildItem” cmdlet, it lists all the child items with their subdirectories. But, when you add the “-Depth” parameter, you can list out the exact level of the content of subdirectories.

For instance, if you specify “-Depth 2” in the Get-ChildItem command, the cmdlet will list the first level subdirectories with the second level subdirectories.

> Get-ChildItem -Path E:\vbox -Recurse -Depth 2

Example 7: Counting the number of child items in a directory

As we save more and more files on our systems, it’s easy to lose track of how many files are in each folder. Get-ChildItem command also provides a solution for that.

We will show you how the Get-ChildItem cmdlet measures the object count from the specified “E:\vbox\” directory.

> (Get-ChildItem -Recurse -Path E:\vbox\ | Measure-Object).Count

Conclusion

PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function. In this article, we have compiled examples to understand listing files in PowerShell. These examples include listing files and folders from a specific directory, including or excluding some particular files, sorting the file list, or controlling the recursion of the directories.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.