E1Tips Qucik Tip

How to Print a Directory Structure Using PowerShell: Step-by-Step Guide

If you need to print out the directory structure and list the files within, this PowerShell script will help you do just that. Follow the steps below to generate a detailed directory tree and save it to a file.

PowerShell Script to Print Directory Structure

To print and save the directory structure of a folder, use the following PowerShell script:

Tree "C:\Root" /f /a | Select-Object -Skip 5 | Set-Content C:\Root\dir.txt

Explanation of the Script Components

  • Tree “C:\Root” /f /a: This command generates a directory tree.
    • /f: Displays the names of the files in each directory.
    • /a: Uses text characters instead of graphic characters to show the lines that link subdirectories.
  • Select-Object -Skip 5: Skips the first 5 lines of the output, which usually contains header information.
  • Set-Content C:\Root\dir.txt: Saves the output to a file named dir.txt in the C:\Root directory.

Sample Output

The script will produce a text file (dir.txt) with the following structure:

+---Test1
|   |   File1.txt
|   |   File2.txt
|   |   File3.txt
|   |   
|   +---Test1.1
|   |       File1.txt
|   |       File2.txt
|   |       
|   \---Test1.2
|           File1.txt
|           
+---Test2
|       File1.txt
|       File2.txt
|       File3.txt
|       
+---Test3
|   \---Test3.1
|           File1.txt
|           File2.txt
|           
\---Test4
        File1.txt

Why Use This Script?

This PowerShell script is especially useful for:

  • Documenting directory structures for project documentation.
  • Explaining directory layouts to both technical and non-technical colleagues.
  • Auditing file and folder structures for organization and management.

Conclusion

By using this PowerShell script, you can easily create a visual representation of any directory structure and its contents. This method is efficient and can be a valuable tool for various administrative and documentation tasks.

I hope this guide helps you manage and present your directory structures more effectively!

More posts on PowerShell and other scripts.

Author

Stewart Schatz

Career: Principal CNC Consultant for Syntax Systems Limited specializing Oracle JD Edwards EnterpriseOne and the technology that supports it. Side Hustle: Owner/Operator of E1Tips.com Location: Lancaster, PA USA  What I like to do: Invest in Family, Explore Technology, Lead Teams, Share Knowledge/Experience, Hunt, Hike, etc.

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
100%

One thought on “How to Print a Directory Structure Using PowerShell: Step-by-Step Guide

  1. tree isn’t a powershell command. tree.com is a actually an old CMD utility. Output probably can’t be incorporated into the pipeline.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Previous post VirtualBox Fails After Windows 11 Upgrade (NtCreateFile failed)
Quick_Tip_Linux Next post How to Clean Up Empty Directories Using the find Command in Linux