Exploring Folder Structures (Part 1)

by Mar 16, 2021

Here is a quick sample illustrating how you can discover folder structures. This example takes any root folder path and recursively traverses through its subfolders.

For each subfolder, a new custom object is returned with the file and subfolder count and the relative subfolder path:

# specify the folder that you want to discover
# $home is the root folder of your user profile
# you can use any folder: $path = 'c:\somefolder'
$path = $HOME

# find all subfolders...
Get-ChildItem $path -Directory -Recurse -ErrorAction Ignore   |
ForEach-Object {
  # return custom object with relative subfolder path and
  # file count
    # use GetFiles() to find all files in folder:
    FileCount = $_.GetFiles().Count
    FolderCount = $_.GetDirectories().Count
    FullName  = $_.Fullname.Substring($path.Length+1)

Twitter This Tip! ReTweet this Tip!