SOLVED

powershell create a catalog file

%3CLINGO-SUB%20id%3D%22lingo-sub-1435005%22%20slang%3D%22en-US%22%3Epowershell%20create%20a%20catalog%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1435005%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20new%20to%20powershell.%20I%20am%20looking%20for%20powershell%20script%20to%20generate%20a%20catalog%20file%20which%20display%20all%20the%20subfolders%20and%20files%20underneath%2C%20better%20to%20comment%20out%20the%20subfolders%2C%20and%20only%20files%20listed%2C%20but%20should%20be%20listed%20just%20under%20the%20sunfolders.%3C%2FP%3E%3CP%3Elet%20say%20I%20have%20a%20folder%20called%20total_folder%3C%2FP%3E%3CP%3Eunderneath%20there%20are%20many%20subfolders%3C%2FP%3E%3CP%3Efolder1%3C%2FP%3E%3CP%3Efile1-1%3C%2FP%3E%3CP%3Efille1-2%3C%2FP%3E%3CP%3Efolder2%3C%2FP%3E%3CP%3Efile2-1%3C%2FP%3E%3CP%3Efile2-2%3C%2FP%3E%3CP%3Efile2-3%3C%2FP%3E%3CP%3Efolder3%3C%2FP%3E%3CP%3Efile3-1%3C%2FP%3E%3CP%3E......%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20I%20would%20like%20to%20document%20these%20info%20into%20a%20catalog%20file%20as%20below%3C%2FP%3E%3CP%3E%3B----folder1----%3C%2FP%3E%3CP%3Efile1-1%3C%2FP%3E%3CP%3Efille1-2%3C%2FP%3E%3CP%3E%3B----folder2----%3C%2FP%3E%3CP%3Efile2-1%3C%2FP%3E%3CP%3Efile2-2%3C%2FP%3E%3CP%3Efile2-3%3C%2FP%3E%3CP%3E%3B----folder3----%3C%2FP%3E%3CP%3Efile3-1%3C%2FP%3E%3CP%3E......%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1435005%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPower%20shell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1435555%22%20slang%3D%22en-US%22%3ERe%3A%20powershell%20create%20a%20catalog%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1435555%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F687212%22%20target%3D%22_blank%22%3E%40goodywp%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20William%2C%3C%2FP%3E%3CP%3EI%20don%E2%80%99t%20know%20why%20you%20want%20it%20like%20this%20but%20it%20is%20possible.%20In%20the%20snippet%20below%20you%E2%80%99ll%20find%20a%20way%20to%20do%20this.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Path%20%3D%20%20%22c%3A%5Ctemp%22%0A%24Outputfile%20%3D%20%22c%3A%5Ctemp%5Coutput.txt%22%0A%24allitems%20%3D%20Get-ChildItem%20-path%20%24Path%20-Recurse%0A%0A%24groups%20%3D%20%24allitems%20%7C%20Group-Object%20-Property%20PSParentPath%0A%24lines%20%3D%20%40()%0Aforeach(%24group%20in%20%24groups)%7B%0A%20%20%20%20%24lines%20%2B%3D%20%24group.name.replace(%22Microsoft.PowerShell.Core%5CFileSystem%3A%3A%22%2C%20%22----%22)%20%2B%20%22----%22%0A%20%20%20%20%24items%20%3D%20%24group.Group%0A%20%20%20%20foreach(%24item%20in%20%24items)%7B%0A%20%20%20%20%20%20%20%20if(-NOT%20%24item.PSIsContainer)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24lines%20%2B%3D%20%24item.name%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%24lines%20%7C%20out-file%20-FilePath%20%24Outputfile%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EIt%E2%80%99s%20also%20possible%20to%20export%20to%20data%20to%20a%20XML%20file%20for%20later%20analysis%20(in%20powershell).%20You%20can%20import%20this%20data%20later%20(also%20on%20a%20different%20device).%20You%20will%20keep%20all%20properties%20like%20size%20(Length)%2C%20dates%20(Create%2C%20LastAccessTime%2C%20LastWritetime%20etc)%20and%20many%20more.%20Disadvantage%20is%20that%20the%20output%20XML%20file%20is%20bigger.%20See%20the%20snippet%20below%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Get%20and%20export%20data%20%0A%24allitems%20%3D%20Get-ChildItem%20-path%20%24Path%20-Recurse%0A%24allitems%20%7C%20Export-Clixml%20%22c%3A%5Ctemp%5Coutput.xml%22%20%0A%0A%23%20Import%20data%0A%24importedItems%20%3D%20Import-Clixml%20%22c%3A%5Ctemp%5Coutput.xml%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20it%20works%20out%20for%20you%20William!%3C%2FP%3E%3CP%3EGood%20luck!%3C%2FP%3E%3CP%3EGrtz%2C%20Manfred%20de%20Laat%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1436335%22%20slang%3D%22en-US%22%3ERe%3A%20powershell%20create%20a%20catalog%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1436335%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412431%22%20target%3D%22_blank%22%3E%40Manfred101%3C%2FA%3E%26nbsp%3BThanks%20sooooo%20much!%3C%2FP%3E%3CP%3EIt%20works%20as%20expected.%20Now%20a%20couple%20of%20enhancements%20as%20below%3A%3C%2FP%3E%3CP%3E1)%20Can%20we%20ignore%20the%20path%2C%20only%20folder%20display%3C%2FP%3E%3CP%3E2)%20Can%20we%20ignore%20the%20parent%20folder%2C%20in%20this%20case%20is%20temp%20folder%20in%20your%20code%2C%20only%20subfolders%20under%20temp%3C%2FP%3E%3CP%3E3)%20Can%20we%20comment%20out%20certain%20file%20with%20certain%20extension%2C%20i.e%2C%20*.txt%20file%20will%20be%20comment%20out%20with%20%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20again%20for%20your%20help!!%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi all,

 

I am new to powershell. I am looking for powershell script to generate a catalog file which display all the subfolders and files underneath, better to comment out the subfolders, and only files listed, but should be listed just under the sunfolders.

let say I have a folder called total_folder

underneath there are many subfolders

folder1

file1-1

fille1-2

folder2

file2-1

file2-2

file2-3

folder3

file3-1

......

 

Now I would like to document these info into a catalog file as below

;----folder1----

file1-1

fille1-2

;----folder2----

file2-1

file2-2

file2-3

;----folder3----

file3-1

......

2 Replies
Best Response confirmed by goodywp (New Contributor)
Solution

@goodywp 

Hi William,

I don’t know why you want it like this but it is possible. In the snippet below you’ll find a way to do this.

$Path =  "c:\temp"
$Outputfile = "c:\temp\output.txt"
$allitems = Get-ChildItem -path $Path -Recurse

$groups = $allitems | Group-Object -Property PSParentPath
$lines = @()
foreach($group in $groups){
    $lines += $group.name.replace("Microsoft.PowerShell.Core\FileSystem::", "----") + "----"
    $items = $group.Group
    foreach($item in $items){
        if(-NOT $item.PSIsContainer){
            $lines += $item.name
        }
    }
}
$lines | out-file -FilePath $Outputfile

It’s also possible to export to data to a XML file for later analysis (in powershell). You can import this data later (also on a different device). You will keep all properties like size (Length), dates (Create, LastAccessTime, LastWritetime etc) and many more. Disadvantage is that the output XML file is bigger. See the snippet below:

# Get and export data 
$allitems = Get-ChildItem -path $Path -Recurse
$allitems | Export-Clixml "c:\temp\output.xml" 

# Import data
$importedItems = Import-Clixml "c:\temp\output.xml"

 

I hope it works out for you William!

Good luck!

Grtz, Manfred de Laat

@Manfred101 Thanks sooooo much!

It works as expected. Now a couple of enhancements as below:

1) Can we ignore the path, only folder display

2) Can we ignore the parent folder, in this case is temp folder in your code, only subfolders under temp

3) Can we comment out certain file with certain extension, i.e, *.txt file will be comment out with ;

 

Thanks again for your help!!