I am looking to export a folder structure from a server and it's kind of huge. So I am looking to narrow the list to a depth of 3 folders max and show no files. So actually I have a part of the answer here already using Get-ChildItem. But I am missing the parameter to narrow down the list. Any idea ?
Thanks
EDIT: Export to a text file or excel sheet - No robocopy
Use the -Directory flag to get only folders, -Recurse to include subfolders, and -Depth to control how far down to go. 0 is current directory folders, 1 includes their subfolders etc.
You can combine with Select-Object to specify the properties to select, and Export-Csv or Out-File to create a csv/txt file.
Get-ChildItem -Recurse -Directory -Depth 3 |
Select-Object FullName |
Export-Csv Test.csv -NoTypeInformation
Ref: Get-ChildItem, Select-Object, Export-Csv
There's no need for Powershell scripting, as Robocopy is part of default install in newer Windows versions. It supports copying partial directory trees. The switch /lev:<N> copies only the top N levels of the source directory tree.
Additional switches like /e (empty directories) and /xf * (exclude all files) are useful to copy just the directory structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With