get-childitem output

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Sam Boutros Sam Boutros 2 years, 4 months ago.

  • Author
    Posts
  • #17144
    Profile photo of Frank Gray
    Frank Gray
    Participant

    I'm trying to get people to clean up old files. Never an easy job. To help them out, I want to generate reports of their department folders. One report would list their Powerpoint files, another PDFs and a third, zip files. These are the space hogs.

    I'm new at Powershell. I do know how to filter for these file types, but I can't figure out how to generate an easy-to-read output. I have a program called Duplicate File Finder. It exports the findings into a slick HTML report with columns like this:

    File Name Size Modified Date In Folder
    ————————————————————-

    I'm also having them review duplicate files within their department folders.

    Since they'll see this slick output from that program , I'd like to generate something similar in Powershell to make it easier for them to recognize files no longer needed even though these won't be duplicates, just outdated files.

    Thanks for any suggestions.

  • #17146
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Use Get-ChildItem to get all desired file types. Sort by filename extension and then by file size, if you like; use Format-Table to group by filename extension, which will separate the different file types into their own sub-tables. That output won't be suitable for an HTML report; you could look at our free ebook, "Creating HTML Reports in Windows PowerShell," to produce more attractive HTML than ConvertTo-HTML will do by itself.

  • #17172
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    Try this script (portions borrowed from EnhancedHTML2-Demo.ps1)
    Requires EnahncedHTML2 module – available for download from [url]https://onedrive.live.com/?cid=7F868AA697B937FE&id=7F868AA697B937FE%21113[/url]
    I've attached the script as an attachment – I see some characters have changed from copy/paste..

    # Script to get list of certain files by extension, and compile HTML reports
    # May be useful when considering file/diskspace cleanup
    # Requires EnahncedHTML2 module avaialble from [url]https://onedrive.live.com/?cid=7F868AA697B937FE&id=7F868AA697B937FE%21113[/url]
    # Sam Boutros 7/15/2014 - V1.0
    #
    # Requires EnhancedHTML2
    Import-Module EnhancedHTML2
    # 
    $FileTypes = @("PP?","ZIP","PDF") # These are the file types we need to include in this report
    $TargetFolders = @("d:\","\\localhost\d$\test2") # These are the folders that we will search in
    $Props = @("Name","DirectoryName","Extension","Length","CreationTime","LastAccessTime","LastWriteTime","Attributes") # These are the file properties that we may be interested in
    $MinFileSize = 1MB # If you want to show files larger than a certain size, enter it here, otherwise enter 0 to pick up all files for the above types.
    # End Data Entry section
    #
    $Style = @"
    
    body {
        color:#333333;
        font-family:Calibri,Tahoma;
        font-size: 10pt;
    }
    h1 {
        text-align:center;
    }
    h2 {
        border-top:1px solid #666666;
    }
    
    th {
        font-weight:bold;
        color:#eeeeee;
        background-color:#333333;
        cursor:pointer;
    }
    .odd  { background-color:#ffffff; }
    .even { background-color:#dddddd; }
    .paginate_enabled_next, .paginate_enabled_previous {
        cursor:pointer; 
        border:1px solid #222222; 
        background-color:#dddddd; 
        padding:2px; 
        margin:4px;
        border-radius:2px;
    }
    .paginate_disabled_previous, .paginate_disabled_next {
        color:#666666; 
        cursor:pointer;
        background-color:#dddddd; 
        padding:2px; 
        margin:4px;
        border-radius:2px;
    }
    .dataTables_info { margin-bottom:4px; }
    .sectionheader { cursor:pointer; }
    .sectionheader:hover { color:red; }
    .grid { width:100% }
    .red {
        color:red;
        font-weight:bold;
    } 
    
    "@
    #
    $HTMLFile = ".\Report" + (Get-Date -format yyyyMMdd_hhmmsstt) + ".html"
    $Tables = @(); $TargetFoldersString = $null
    foreach ($TargetFolder in $TargetFolders) {
        $TargetFoldersString += $TargetFolder + " - "
        $FileTypesString =$null
        foreach ($FileType in $FileTypes) {
            Write-Output "Searching for files with $FileType extension on folder $TargetFolder" 
            $FileTypesString += $FileType + " - "
            $Files = Get-ChildItem -Path $TargetFolder -Include *.$FileType -Force -Recurse -ErrorAction SilentlyContinue
            $FileList = @()
            ForEach ($File in $Files) {
                if ($File.Length -gt $MinFileSize) {
                    $MyObjProps = @{}; foreach ($Prop in $Props) {$Temp = $File.$Prop; $MyObjProps.Add("$Prop", "$Temp")} # Loop 4 - File properties
                    $MyFile = New-Object -TypeName PSObject -Property $MyObjProps
                    $FileList += $MyFile
                }
            } # Loop 3 - Files
            if ($FileList.Count -gt 0) { # Don't need empty tables
                $Params = @{'As'='Table';
                        'PreContent'='''' + $FileList.Count + ''' files with ''' + $FileType + ''' extension under folder ''' + $TargetFolder + '''';
                        'EvenRowCssClass'='even';
                        'OddRowCssClass'='odd';
                        'MakeTableDynamic'=$true;
                        'TableCssClass'='grid';
                        'Properties'=$Props }
                $Table = $FileList | ConvertTo-EnhancedHTMLFragment @params
                $Tables += $Table
            }
        } # Loop 2 - File Types
    } # Loop 1 - Search Folders
    $PreContent = "Report of files with '" + $FileTypesString.Substring(0,$FileTypesString.Length-3) + "' extension(s)"
    $PreContent += " on folder(s) '" + $TargetFoldersString.Substring(0,$TargetFoldersString.Length-3) + "'"
    if ($MinFileSize -gt 0) {$PreContent += "Listing only files larger than " + '{0:N0}' -f ($MinFileSize/1MB) + " MB"}
    $Params = @{'CssStyleSheet'=$Style;
            'Title'="File Types Report";
            'PreContent'=$PreContent;
            'HTMLFragments'=$Tables;
            'jQueryDataTableUri'='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.3/jquery.dataTables.min.js';
            'jQueryUri'='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js'} 
    ConvertTo-EnhancedHTML @params | Out-File -FilePath $HTMLFile
    If ($Error.Count -gt 0) {
        $ErrorLog = ".\Report_Errors" + (Get-Date -format yyyyMMdd_hhmmsstt) + ".txt"
        "Errors encountered: " | Out-File $ErrorLog
        $Error | Out-File $ErrorLog -Append
        "Expect to see errors for file paths > 260 characters, and folders where you have no NTFS permissions" | Out-File $ErrorLog -Append
        "Those files are not included in the report" | Out-File $ErrorLog -Append
        Write-Host "Errors encountered: $Error" -ForegroundColor Yellow 
        Write-Host "Expect to see errors for file paths > 260 characters, and folders where you have no NTFS permissions" -ForegroundColor Yellow
        Write-Host "Those files are not included in the report"
    } 
    Write-Host "Done. Report saved to file $HTMLFile" -ForegroundColor Cyan
    

You must be logged in to reply to this topic.