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.

First part of the script.

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 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 variable. Inside the loop it uses 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 , Length(Size), LastWriteTime, and . 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 .

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 , than the object is a directory and then use 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.

The process part of the script.

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.

The complete script.

Hope you enjoyed this little modification.

Originally published at



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store