Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Export structure of folders to txt or xls but only to a depth of 3 folders and no files

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

like image 540
RazZ Avatar asked Jul 25 '17 07:07

RazZ


2 Answers

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

like image 93
G42 Avatar answered Oct 21 '22 01:10

G42


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.

like image 32
vonPryz Avatar answered Oct 20 '22 23:10

vonPryz