PowerShell script to give me a full disk report and name the report based on user input

Problem:

I have been using a gui based tool for years to report on my backup disk media, outputting several key items of information.  I wanted to do all of this in PowerShell and automate it all.

Solution:

The below script will:

1. Ask the user to select an item from a drop down menu, this variable becomes the filename.

2. Collects total disk space and converts to GB, then exports to .csv

3. Collects free disk space and converts to GB, then exports (appends) to the .csv above

4. Collects summary folder and file size information and outputs to a new .csv

5. Collects all file/folder data and outputs to a .csv

6. Merges all .csv files and names based on the variable collected at the start

$DiskPicker = @("NAS2A","NAS2B","NAS3A","NAS3B","NAS4A","NAS4B","NAS5A","NAS5B","NAS5C")
$DiskDest = $DiskPicker | Out-GridView -Title "Which disk do you want to report on?" -Passthru

$DiskR = Get-WmiObject -Class Win32_logicaldisk -Filter "DeviceID = 'D:'" 

$DiskRSize = $DiskR | select Size -ExpandProperty Size

$DiskRSizeTB = $DiskRSize /1Gb

$A = "Total Disk space";
$B = $DiskRSizeTB;

$finalpath = ("\\192.168.0.6\documents\Backups\" + $DiskDest + ".csv")
$path = "C:\System\Backups\CSV\"
$path1 = ($path + $DiskDest + "1.csv")

$wrapper = New-Object PSObject -Property @{ Type = $A; Disk = $B }
Export-Csv -InputObject $wrapper -Path $path1  -NoTypeInformation -force


$DiskRSizeF = $DiskR | select Freespace -ExpandProperty Freespace

$DiskRSizeFTB = $DiskRSizeF /1Gb

$A = "Free Disk space";
$B = $DiskRSizeFTB;

$wrapper = New-Object PSObject -Property @{ Type = $A; Disk = $B }
Export-Csv -InputObject $wrapper -Path $path1 -NoTypeInformation -Append


$path2 = ($path + $DiskDest + "2.csv")

Get-ChildItem -Path D:\ -Recurse | sort FullName | select Fullname,CreationTime,LastWriteTime | Export-Csv $path2 -nti -force




$RootFolder = 'D:\'

$path3 = ($path + $DiskDest + "3.csv")

function Get-FormattedSize{
    param(
        [parameter( Mandatory = $true )]
        [int64]$Size
        )

    switch( $Size ){
        { $Size -gt 1TB }{ '{0:N2} TB' -f ( $Size  / 1TB ); break }
        { $Size -gt 1GB }{ '{0:N2} GB' -f ( $Size  / 1GB ); break }
        { $Size -gt 1MB }{ '{0:N2} MB' -f ( $Size  / 1MB ); break }
        { $Size -gt 1KB }{ '{0:N2} KB' -f ( $Size  / 1KB ); break }
        default { "$Size B"; break }
        }
    }

$Results = New-Object -TypeName System.Collections.ArrayList

$RootSize = Get-ChildItem -Path $RootFolder -Recurse |
        Where-Object { -not $_.PSIsContainer } |
        Measure-Object -Property Length -Sum |
        Select-Object -ExpandProperty Sum

$null = $Results.Add(( New-Object -TypeName psobject -Property @{
    Path = $RootFolder
    Size = Get-FormattedSize -Size $RootSize
    } ))

$null = $Results.Add( '' )

$Folders = Get-ChildItem -Path $RootFolder |
    Where-Object { $_.PSIsContainer } |
    Select-Object -ExpandProperty FullName

$null = foreach( $Folder in $Folders ){
    $FolderSize = Get-ChildItem -Path $Folder -Recurse |
        Where-Object { -not $_.PSIsContainer } |
        Measure-Object -Property Length -Sum |
        Select-Object -ExpandProperty Sum

    $Results.Add(( New-Object -TypeName psobject -Property @{
    Path = $Folder
    Size = Get-FormattedSize -Size $FolderSize
    } ))

    $Files = Get-ChildItem -Path $Folder | Where-Object { -not $_.PSIsContainer }

    foreach( $File in $Files ){
        $Results.Add(( New-Object -TypeName psobject -Property @{
            Path = $File.Name
            Size = Get-FormattedSize -Size $File.Length
            } ))
        }

    $Results.Add( '' )
    }

$Results |
    Select-Object -Property Path, Size | Export-Csv $path3 -nti -force


New-Item -ItemType file $finalpath force
Get-Content $path1, $path2, $path3 | Add-Content $finalpath

Leave a Reply

Your email address will not be published. Required fields are marked *