Create a Directory Tree with Powershell
I was playing around with PSDrives when I decided to write this script. It recurses through (part of) a PSDrive and shows the items it finds in a simple tree view.
The fun part is that it works on any PSDrive!
- Show-Tree D:\scripts will show subdirectories and files in a tree;
- Show-Tree HKCU:\ will show Registry keys and subkeys in a tree;
- Show-Tree VI:\ will show your VMware Virtual Infrastructure in a tree, if you first create a PSDrive for it.
Here’s how to create a PSDrive for you Virtual Infrastructure:
Connect-VIServer MYVISERVER
$root = Get-Folder -NoRecursion
New-PSDrive -Name VI -PSProvider VIMInventory -Root ‘\’ -Location $root
Creating a full tree takes plenty of time, but you can get a branch of the full tree like so: Show-Tree VI:\MYDATACENTER\host\MYFOLDER\MYCLUSTER, where you replace the caps with your own names. Use vm instead of host to show the Virtual Machines and Templates view instead of the Hosts and Clusters view.
Hugo
Param($BaseDir)
# Function that lists the children of a dir
function Show-TreeItems
{
Param($Location)# Function that indents to a level i
function Indent
{
Param([Int]$i)
$Global:Indent = $null
For ($x=1; $x -le $i; $x++)
{
$Global:Indent += ” “
}
}$Children = Get-ChildItem $Location | Sort Name
ForEach ($Child in $Children)
{
Indent $i
“{0}{1}” -f $Indent,$Child
If ($Child.PSIsContainer)
{
# Recurse through subdir
$i++
Show-TreeItems $Child.PSPath
$i– # NOTE THERE SHOULD BE TWO MINUS SIGNS HERE
}
}}
# Actual start of script
$i = 0
Show-TreeItems $BaseDir
4 Responses to Create a Directory Tree with Powershell
Leave a Reply Cancel reply
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenRecent Comments
- Ben @ Geekswing on Helpful script of the day: HA calculations
- Jo Lambrecht on WSUS Cleanup with Powershell
- Luke on Powershell Open File Dialog Box
- Umapathi on WSUS Cleanup with Powershell
- Yomodo on User Confirmation in Powershell
Archives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





Hi, another great one
Just one note: $i- has to be $i–
Thanks for posting it. That’s one of the scripts I wanted to create but you save my time.
david
Thank you David,
You are absolutely right about the $i – - . For some reason the site software removes the second minus sign.
Hugo
[...] This was quite a challenge. But I did it! Inspired by my scripts Create a Directory Tree with Powershell and Listing AD Group Members Recursively with Powershell, I responded to a queation in the VI [...]
Excellent. I found the output easier to read if I replaced
$Global:Indent += ” “with$Global:Indent += [char]9 #tab