Color coded directory listing with PowerShell


Created a script with a modification of the Get-ChildItem cmdlet, which reads a command line parameter which should specify the path to a folder on your system. The script lists the content of the folder just like Get-ChildItem, except that the output is color-coded using yellow text for directories and green text for files.

Breaking down the script

The first part of the code deals with creating an advanced function and handling the parameter of the script. Within the parameter attribute I specify that the parameter is required by setting mandatory to true, adding a help message and validates that the parameter only accepts string values.

function Get-ChildItemColorized {
    [CmdletBinding()]
    param (
        # The folder path
        [Parameter(Mandatory = $True, 
            HelpMessage = "Enter a directory path")]
        [String] 
        $FolderPath
    )
}

If you run the script without a parameter, you will get prompted with the following message:

I usually use Visual Studio Code when creating and running scripts, and I have found out that there is a difference between the command line in Visual Studio Code and the regular PowerShell console. In Visual Studio Code you don’t get the help message, while in the PowerShell console you get the opportunity to view the help message.

When using a regular PowerShell console it looks like the following when you first run the script without specifying a parameter, then ask for help by typing !?, and finally type the folder path.

The last part is where the colorization and the action happens, inside the process block.

The first line outputs the message “Directory listing of” and than the given folder path, with `n to get a new line after the parameter input.

Then I’m looping through the specified directory with foreach and the Get-ChildItem cmdlet. This grabs a list of item in the path specified in the parameter. Each iteration of the loop brings back one item which is stored temporarily in the $Item variable. Inside the loop it uses $Item to refer to the single object returned by this particular iteration of the loop.

Each item in a folder contains a set of properties such as its Name, Length(Size), LastWriteTime, and Mode. Mode refers to the different attributes of that item, such as directory (d), archive (a), read-only (r), hidden (h), and system (s). Each attribute is given its specific spot within this sequence (darhs), and any attribute that isn’t set is instead replaced with a dash. For instance a read-only directory will get d- - r - -.

So by using the mode property and know that the item is a directory, we know that the first character in this value is d. In this script this is exactly what we want, to check the first character in the mode property and than check if it’s value is d. If it the mode property is d, than the object is a directory and then use Write-Host to display the name with the foreground color of yellow; otherwise, the object is a file and you display the name with the foreground color of green.

process {
    Write-Host ("`nDirectory listing of $FolderPath")

    # Process each item in the directory
    foreach ($Item in Get-ChildItem $FolderPath) {
        if ($Item.mode.substring(0, 1) -eq "d") {
            Write-Host $Item.name -ForegroundColor "Yellow"
        }
        else {
            Write-Host $Item.name -ForegroundColor "Green"
        }
    }
}

Here is the full script. To run the save it in a desired place, load it into memory by using dot source for instance . C:\ScriptsGet-ChildItemColorized. Then run the script as a regular cmdlet, like so Get-ChildItemColorized -FolderPath C:\ then you’ll get a list as the image above.

function Get-ChildItemColorized {
    [CmdletBinding()]
    param (
        # The folder path
        [Parameter(Mandatory = $True,
            HelpMessage = "Enter a path directory path")]
        [String]
        $FolderPath
    )
    process {
        Write-Host ("`nDirectory listing of $FolderPath")

        # Process each item in the directory
        foreach ($Item in Get-ChildItem $FolderPath) {
            if ($Item.mode.substring(0, 1) -eq "d") {
                    Write-Host $Item.name -ForegroundColor "Yellow"
            }
            else {
                Write-Host $Item.name -ForegroundColor "Green"
            }
        }
    }
}

Hope you enjoyed this little modification.