Home » PowerShell » PowerShell ls – List files and Directories

PowerShell ls – List files and Directories

ls command in Linux is used to list files and directories. In PowerShell, the ls command can be used to get the list of files and directories and their information. Unix equivalent ls command alias in PowerShell are Get-ChildItem, dir, and GCI.

PowerShell ls - list files and directories
PowerShell ls – list files and directories

PowerShell ls command like Get-ChildItem cmdlet is used to list files and directories, filter files and directories, and sort files and directories.

In this article, we will discuss how to use the PowerShell ls command to get the list of the files and directories and perform other operations like filter files, sort files, etc…

Use the PowerShell ls command to retrieve list files and directories

Unix equivalent ls command in PowerShell can be used to retrieve the list of files and directories.

Refer to the following example where the PowerShell ls command is used to get a list of files and directories.

 ls -Path 'D:\PS\Config\' 

In the above PowerShell script, the ls command uses the Path parameter to specify the directory path. It fetches all the files and directories from the directory.

The output of the above PowerShell ls command lists the files and provides information about each object like Mode, LastWriteTime, Length, and Name.

PS D:\> ls -Path 'D:\PS\Config\'                                                                       

    Directory: D:\PS\Config


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       07-08-2022     17:34            388 app-1.config
-a----       16-08-2022     09:28             32 app-1.ps1
-a----       07-08-2022     17:34           1378 app-2.config
-a----       07-08-2022     17:34           1378 app-3.config
-a----       07-08-2022     17:34            388 Get-PI.ps1


PS D:\> 

Use the PowerShell ls command to filter files and directories

PowerShell ls command can be used to filter the files and directories from the path-specified directory. It uses the filter parameter and condition.

Refer to the following example where the PowerShell ls command filters files and directories based on the conditions “*.txt”.

ls -Path "D:\PS\" -Filter "*.txt"

In the above PowerShell script, the ls command uses the Path parameter to specify the directory from where it needs to find the files having extension .txt

The output of the above PowerShell script is:

PS D:\> ls -Path "D:\PS\" -Filter "*.txt"                                                              

    Directory: D:\PS


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       31-07-2022     17:12          13874 Alias.txt
-a----       12-08-2022     16:21             49 calc.txt
-a----       21-01-2023     11:24            720 employee.txt
-a----       07-08-2022     14:09            193 PS-String.txt


PS D:\>  

Use the ls command to sort the files and directories

The ls command is a commonly used command for working with files and directories. In PowerShell, the ls command can be used to sort the files and directories based on the specified conditions.

ls -Path "D:\PS\"| Sort Name

In the above PowerShell script, the ls command uses the Path parameter to specify the directory to get the list of files and directories. It uses the pipe operator to send output to the Sort command to order the file by their Name.

The output of the above PowerShell script to list the files in order by their name is:

PS D:\> ls -Path "D:\PS\"| Sort Name


    Directory: D:\PS


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       16-07-2022     22:08                AD
-a----       31-07-2022     17:12          13874 Alias.txt
-a----       07-08-2022     10:38           1378 app.config
-a----       12-08-2022     16:21             49 calc.txt
-a----       24-06-2022     08:36           5058 cmd-get-username.png
d-----       15-08-2022     20:06                Config
d-----       15-08-2022     20:10                Config-Backup

PowerShell Tip: How to list files sorted by date in PowerShell!

Conclusion

I hope the above article on how to use the PowerShell ls command to list the files, directories, filter, and sort the files is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.