Skip to content

Commit

Permalink
made file totals optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gngrninja committed Dec 11, 2018
1 parent a20de8f commit d6de99e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 20 deletions.
61 changes: 50 additions & 11 deletions PSFolderSize/Functions/Public/Get-FolderSize.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ function Get-FolderSize {
Example: C:\users\you\desktop\output.csv
.PARAMETER AddFileTotals
This parameter allows you to add file totals to the results.
Note: This will reduce performance of the script by around 30%!
.EXAMPLE
Get-FolderSize | Format-Table -AutoSize
Expand Down Expand Up @@ -128,6 +133,23 @@ function Get-FolderSize {
Omit folder(s) from being included
Get-FolderSize.ps1 -OmitFolders 'C:\Temp','C:\Windows'
.EXAMPLE
Add file counts for each folder
Note: This will slow down the execution of the script by around 30%
$results = Get-FolderSize -AddFileTotal
PS /Users/ninja/Documents/repos/PSFolderSize> $results[0] | Format-List *
FolderName : .git
Size(Bytes) : 228591
Size(MB) : 0.22
Size(GB) : 0.00
FullPath : /Users/ninja/Documents/repos/PSFolderSize/.git
HostName : njambp.local
FileCount : 382
#>
[cmdletbinding(
DefaultParameterSetName = 'default'
Expand Down Expand Up @@ -163,6 +185,12 @@ function Get-FolderSize {
[Switch]
$AddTotal,

[Parameter(
ParameterSetName = 'default'
)]
[Switch]
$AddFileTotals,

[Parameter(
ParameterSetName = 'default'
)]
Expand Down Expand Up @@ -236,12 +264,10 @@ function Get-FolderSize {

$folderInfo = Get-Childitem -LiteralPath $fullPath -Recurse -Force -ErrorAction SilentlyContinue
$folderSize = $folderInfo | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue
$totalFiles = ($folderInfo | Where-Object {!$_.PSIsContainer}).Count
#We use the string format operator here to show only 2 decimals, and do some PS Math.
$folderSizeInBytes = $folderSize.Sum
$folderSizeInMB = "{0:N2}" -f ($folderSize.Sum / 1MB)
$folderSizeInGB = "{0:N2}" -f ($folderSize.Sum / 1GB)


}

Expand All @@ -253,12 +279,19 @@ function Get-FolderSize {
'Size(Bytes)' = $folderSizeInBytes
'Size(MB)' = $folderSizeInMB
'Size(GB)' = $folderSizeInGB
FileCount = $totalFiles
FullPath = $fullPath
HostName = $hostName

}

#Add file totals if switch is true
if ($AddFileTotals) {

$totalFiles = ($folderInfo | Where-Object {!$_.PSIsContainer}).Count
$folderObject | Add-Member -MemberType NoteProperty -Name FileCount -Value $totalFiles

}

#Add the object to the array
$folderList.Add($folderObject) | Out-Null

Expand All @@ -277,12 +310,6 @@ function Get-FolderSize {

}

$folderList | ForEach-Object {

$grandTotalFiles += $_.FileCount

}

$totalFolderSizeInMB = "{0:N2}" -f ($grandTotal / 1MB)
$totalFolderSizeInGB = "{0:N2}" -f ($grandTotal / 1GB)

Expand All @@ -292,16 +319,28 @@ function Get-FolderSize {
'Size(Bytes)' = $grandTotal
'Size(MB)' = $totalFolderSizeInMB
'Size(GB)' = $totalFolderSizeInGB
FileCount = $grandTotalFiles
FullPath = 'N/A'
HostName = $hostName

}

if ($AddFileTotals) {

$folderList | ForEach-Object {

$grandTotalFiles += $_.FileCount

}

$folderObject |
Add-Member -MemberType NoteProperty -Name FileCount -Value $grandTotalFiles

}

#Add the object to the array
$folderList.Add($folderObject) | Out-Null
}

}
}

if ($Output -or $OutputFile) {
Expand Down
6 changes: 0 additions & 6 deletions PSFolderSize/PSFolderSize.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
<TableColumnHeader>
<Width>12</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>12</Width>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
</TableHeaders>
Expand All @@ -36,9 +33,6 @@
<TableColumnItem>
<PropertyName>Size(GB)</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>FileCount</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>FullPath</PropertyName>
</TableColumnItem>
Expand Down
5 changes: 3 additions & 2 deletions docs/reference/functions/Get-FileReport.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Get-FileReport [-Output <String>] [-OutputPath <String>] [<CommonParameters>]
## EXAMPLES

### Example 1
```powershell
```
PS C:\> {{ Add example code here }}
```

Expand All @@ -48,7 +48,7 @@ Aliases:

Required: False
Position: Named
Default value: None
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
Expand Down Expand Up @@ -170,6 +170,7 @@ For more information, see about_CommonParameters (http://go.microsoft.com/fwlink
## OUTPUTS
### System.Object
## NOTES
## RELATED LINKS
38 changes: 37 additions & 1 deletion docs/reference/functions/Get-FolderSize.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ You can change the base path, omit folders, as well as output results in various
### default (Default)
```
Get-FolderSize [[-BasePath] <String[]>] [-FolderName <String[]>] [-OmitFolders <String[]>] [-AddTotal]
[-UseRobo] [-Output <String>] [-OutputPath <String>] [-OutputFile <String>] [<CommonParameters>]
[-AddFileTotals] [-UseRobo] [-Output <String>] [-OutputPath <String>] [-OutputFile <String>]
[<CommonParameters>]
```

### outputWithType
Expand Down Expand Up @@ -119,6 +120,25 @@ Omit folder(s) from being included

Get-FolderSize.ps1 -OmitFolders 'C:\Temp','C:\Windows'

### EXAMPLE 8
```
Add file counts for each folder
```

Note: This will slow down the execution of the script by around 30%

$results = Get-FolderSize -AddFileTotal

PS /Users/ninja/Documents/repos/PSFolderSize\> $results\[0\] | Format-List *

FolderName : .git
Size(Bytes) : 228591
Size(MB) : 0.22
Size(GB) : 0.00
FullPath : /Users/ninja/Documents/repos/PSFolderSize/.git
HostName : njambp.local
FileCount : 382

## PARAMETERS

### -BasePath
Expand Down Expand Up @@ -182,6 +202,22 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -AddFileTotals
This parameter allows you to add file totals to the results.
Note: This will reduce performance of the script by around 30%!
```yaml
Type: SwitchParameter
Parameter Sets: default
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### -UseRobo
{{Fill UseRobo Description}}
Expand Down
2 changes: 2 additions & 0 deletions psake.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ task Pester -Depends Build {

$env:PSModulePath = $origModulePath

Remove-Item -Path $testResultsXml -Force

} -description 'Run Pester tests'

task CreateMarkdownHelp {
Expand Down

0 comments on commit d6de99e

Please sign in to comment.