Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Created January 28, 2019 16:40
Show Gist options
  • Save jdhitsolutions/cbdc7118f24ba551a0bb325664415649 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/cbdc7118f24ba551a0bb325664415649 to your computer and use it in GitHub Desktop.
A PowerShell function to get the size and number of files in the hidden .git directory.
Function Get-GitSize {
<#
.SYNOPSIS
Get the size of .git folder
.DESCRIPTION
When using git, it creates a hidden folder for change tracking. Because the file is hidden it is easy to overlook how large it might become. This command provides a simple mechanism for calculating the size of the .git folder. Specify the parent folder path.
.PARAMETER Path
The path to the parent folder, not the .git folder.
.EXAMPLE
PS C:\scripts\PiedPiperBox> Get-GitSize
Path Files Size
---- ----- ----
C:\scripts\PiedPiperBox\ 30 18456
Get the size of the .git folder from the current path The size is in bytes.
.EXAMPLE
PS C:\> Get-GitSize C:\scripts\DevOps-Courses\ -as mb
Path Files Size
---- ----- ----
C:\scripts\DevOps-Courses\ 1084 163.680551528931
Get the size of .git under the DevOp-Courses folder formatted as MB.
.EXAMPLE
PS C:\scripts> dir -Directory | Get-GitSize
Path Files Size
---- ----- ----
C:\scripts\ADMXTests 57 33192
C:\scripts\AutoLab2 46 74980
C:\scripts\ComputerCertificates 37 23635
C:\scripts\Copy-Command 83 57112
C:\scripts\Demo Class-Based Tools 175 643881
...
Find all folders in C:\Scripts with a .git directory and calculate the space for each.
.INPUTS
[System.String]
[PSCustomObject]
.NOTES
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/
.LINK
Get-ChildItem
.LINK
Measure-Object
#>
[cmdletbinding()]
Param (
[Parameter(Position = 0, ValueFromPipeline, ValueFromPipelinebyPropertyName)]
[alias("pspath")]
[ValidateScript( {Test-Path $_})]
[string]$Path = ".",
[ValidateSet("kb", "mb", "gb")]
[string]$As
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN] Starting $($myinvocation.mycommand)"
} #begin
Process {
$full = Convert-Path -Path $Path
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing path $full"
$git = Join-Path -Path $full -childpath ".git"
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Testing $git"
if (Test-Path -path $git) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Measuring $git"
#get the total size of all files in the .git folder
$stat = Get-Childitem -path $git -Recurse -File | Measure-Object -Property length -sum
if ($As) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Calculating size as $($As.toUpper())"
$size = $stat.sum / "1$as"
}
else {
$size = $stat.sum
}
#create the output
[PSCustomObject]@{
Path = $full
Files = $stat.count
Size = $Size
} #customobject
} #if test-path
else {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Did not find $git"
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
@jdhitsolutions
Copy link
Author

@jasonrush
Copy link

Would it make sense to limit the number of digits to something like 2 decimal places when using -As?

@jdhitsolutions
Copy link
Author

Would it make sense to limit the number of digits to something like 2 decimal places when using -As?

That is up to you. I'm reluctant to hardcode that as the value because what if someone else wants 4 decimal places? If you need it to be 2 decimal places then process the function output with Select-Object and do it there.

@jdhitsolutions
Copy link
Author

@jdhitsolutions
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment