PowerShell Command Equivalent to Linux ls

Rohan Timalsina Feb 14, 2024
  1. Use the ls Command to List All Files or Directories in Linux
  2. Use Get-ChildItem Cmdlet as a PowerShell Equivalent to Linux ls Command
  3. Use ls, dir or gci as a PowerShell Equivalent to Linux ls Command
  4. Conclusion
PowerShell Command Equivalent to Linux ls

The ls command in Linux is used to list files and directories. If no directory is specified, it displays all files and directories in the current working directory.

You can perform different file and folder tasks in PowerShell, such as listing, creating, copying, moving, and removing files and folders. Some PowerShell commands function similarly to the ls command in Linux, i.e., list files and directories in the directory.

This tutorial will introduce different PowerShell equivalent commands for the Linux ls command.

Use the ls Command to List All Files or Directories in Linux

The following command lists all files and directories in Linux and other Unix-based operating systems.

ls

Output:

powershell equivalent to ls command - output 1

You can use the -l option to view the long format output.

ls -l

Output:

powershell equivalent to ls command - output 2

Use Get-ChildItem Cmdlet as a PowerShell Equivalent to Linux ls Command

In PowerShell, the Get-ChildItem cmdlet serves as the equivalent to the ls command in Unix-like systems, allowing users to list the contents of directories and files. This cmdlet is versatile and offers various parameters for filtering and customizing the output according to specific requirements.

When no directory is provided, it displays all files and directories in the current working directory.

Get-ChildItem

This command, when executed in PowerShell, will display a list of items in the current directory. The output includes various properties of each item, such as the mode (file or directory), last write time, length (for files), and name.

  • Mode: Indicates whether the item is a directory (d) or a file (-).
  • LastWriteTime: Specifies the date and time the item was last modified.
  • Length: Represents the size of the file in bytes. For directories, it usually shows 0.
  • Name: Displays the name of the file or directory.

Output:

powershell equivalent to ls command - output 3

To list files and directories in a specific location, you can use the -Path parameter.

Example:

Get-ChildItem -Path C:\path

When you execute the command Get-ChildItem -Path C:\path, PowerShell retrieves information about the files and directories located at the specified path, which in this case is C:\path. This command essentially acts as a query to the file system, asking for a list of items within the specified directory.

You utilize the Get-ChildItem cmdlet, which is designed to fetch child items (files and directories) within a specified location. The -Path parameter specifies the directory you want to query, in this case, C:\path.

By providing the -Path parameter, you instruct PowerShell to focus its search on the specified directory rather than the current working directory.

Once executed, PowerShell searches the specified directory (C:\path) and returns a list of items found within it. This list includes details such as the mode (type of item), last write time, file size, and name of each item.

Output:

powershell equivalent to ls command - output 4

The ls -a command in Linux is used to list files or directories, including hidden files or directories. In PowerShell’s Get-ChildItem, you can use the -Force parameter to view files or directories, including the hidden ones.

Get-ChildItem -Force

Use ls, dir or gci as a PowerShell Equivalent to Linux ls Command

The ls, dir, and gci are built-in aliases for the Get-ChildItem cmdlet in PowerShell. These aliases provide convenient shortcuts for executing the Get-ChildItem cmdlet without typing out the full command.

You can use any of these aliases interchangeably to achieve the same result: listing files and directories in the PowerShell environment.

ls C:\path

When you execute the ls method in PowerShell, it functions similarly to the Get-ChildItem cmdlet, providing you with a list of files and directories in the specified location. By default, it lists the contents of the current directory.

You can specify additional parameters such as -Filter to narrow down the results or -Recurse to include subdirectories. The -Force parameter can be used to display hidden files and directories.

Output:

powershell equivalent to ls command - output 5

The gci method provides another way to achieve similar functionality to the ls command in Unix-like systems. This method is a shorthand alias for the Get-ChildItem cmdlet, which is used to list the contents of directories and files.

gci "C:\path"

When you execute the gci method in PowerShell, it essentially invokes the Get-ChildItem cmdlet under the hood, providing you with a list of files and directories in the specified location.

Output:

powershell equivalent to ls command - output 6

The dir method is similar to the Unix ls command and allows users to view information about files and directories in a specified location.

dir "C:\path"

When you execute the dir method in PowerShell with a specified path, it retrieves information about the files and directories located on that path. By default, it lists the contents of the specified directory.

Output:

powershell equivalent to ls command - output 7

Conclusion

We explored the PowerShell equivalents to the ls command in Linux, which are crucial for navigating and managing files and directories in a Windows environment. The Get-ChildItem cmdlet serves as the primary method for listing files and directories in PowerShell, offering various parameters such as -Path, -Filter, and -Force for customized output based on specific requirements.

Additionally, we discussed the built-in aliases ls, dir, and gci, which provide shortcuts for invoking the Get-ChildItem cmdlet. These aliases offer flexibility and convenience, making it easier for users to interact with PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Command