Forum Discussion

goodywp's avatar
goodywp
Copper Contributor
Jun 03, 2020
Solved

powershell create a catalog file

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...
  • Manfred101's avatar
    Jun 03, 2020

    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

Resources