powershellls

Powershell's equivalent to Linux/Unix 'ls -al'


Is there a PowerShell equivalent to the ls -al command in Linux/Unix? I tried to find something, but it said there wasn't an equivalent command in PowerShell.


Solution

  • The ls Unix utility's options you're using:

    The PowerShell equivalent is:

    Get-ChildItem -Force
    

    See the bottom section for shortening this command and defining a convenience "alias" (function).


    There's one fundamental difference between ls and Get-ChildItem, however:


    For interactive convenience (you shouldn't do that in scripts), you can use built-in aliases and unambiguous parameter-name prefixes to shorten the command:

    gci -fo
    

    gci is built-in alias for Get-ChildItem, and its name follows PowerShell's naming conventions, where each so-called approved verb also has an approved 1-2 letter alias form; running Get-Verb Get reveals that the approved Get verb's alias form is g; while the noun part isn't formally regulated, remembering that ci stands for ChildItem should be easy.

    -fo is the shortest parameter-name prefix that unambiguously refers to the -Force switch (just -f alone could also refer to -Filter[1])

    To see all aliases defined for a given command (Get-ChildItem in this example):

    PS> Get-Alias -Definition Get-ChildItem
    
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Alias           dir -> Get-ChildItem                                          
    Alias           gci -> Get-ChildItem                                          
    

    To limit output to built-in aliases, i.e. to exclude aliases defined by other modules or $PROFILE scripts, run pwsh -noprofile { Get-Alias -Definition Get-ChildItem } (in Windows PowerShell, use powershell instead of pwsh.

    Therefore, it is advisable to stick to those aliases that are abbreviated forms of the PowerShell command names, such as gci for Get-ChildItem, and gc for Get-Content. While that doesn't initially help with transitioning from familiar names such as ls / dir and cat, the fact that PowerShell cmdlet names as well as their abbreviated aliases are systematic and convention-based makes it easier to memorize them going forward.

    Defining a convenience "alias" with preset arguments, using a function:

    POSIX-compatible shells such as bash allow you to define aliases with preset arguments, such as alias ll='ls -al'

    By contrast, aliases in PowerShell are mere name mappings and you need to declare a function instead (which you can add to your $PROFILE file for availability in future session):

    # Equivalent of bash alias `alias ll='ls -al'`
    function ll { ls -al @args }
    

    Or, an analogous gcih function (h denoting hidden) based on Get-ChildItem:

    function gcih { Get-ChildItem -Force @args }
    

    See this answer for more information about aliases vs. functions in PowerShell, including how to create more sophisticated wrapper functions.


    [1] Conceptually, it could also refer to the -File parameter, but technically the latter is a dynamic parameter, specific to PowerShell's file-system provider, and is therefore not being considered during the ambiguity check.