Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active January 9, 2020 07:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/4b2f0acabf7bd69ac7e9c590273f36b2 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/4b2f0acabf7bd69ac7e9c590273f36b2 to your computer and use it in GitHub Desktop.
An enhanced revision of my Get-GitSize PowerShell function which uses type extensions for additional properties
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. Specify the parent folder path. The result displays the size value in bytes but there are additional properties that show the value formatted in KB, MB or GB.
.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\ | Select Path,Files,SizeMB
Path Files SizeMB
---- ----- ------
C:\scripts\DevOps-Courses\ 29 50.8291053771973
Get the size of the git folder under C:\Scripts\Devops-Courses selecting a different set of properties.
.EXAMPLE
PS C:\> dir c:\scripts -Directory | Get-GitSize | Sort Size -descending | Select -first 5 -property Path,Files,SizeMB
Path Files SizeMB
---- ----- ------
C:\scripts\PS-AutoLab-Env 46 183.732244491577
C:\scripts\DevOps-Courses 29 50.8291053771973
C:\scripts\PSGUI 32 6.39523887634277
C:\scripts\Lab 132 1.67190265655518
C:\scripts\tips 530 1.66895771026611
Get the directories under C:\Scripts that have a .git folder and sort on the Size property in descending order. Then select the first 5 directories and use the specified properties.
.INPUTS
[System.String]
.NOTES
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/
This is a variation of code posted at https://gist.github.com/jdhitsolutions/cbdc7118f24ba551a0bb325664415649
.LINK
Get-ChildItem
.LINK
Measure-Object
#>
[cmdletbinding()]
[Outputtype("gitSize")]
Param (
[Parameter(Position = 0, ValueFromPipeline, ValueFromPipelinebyPropertyName)]
[alias("pspath")]
[ValidateScript( {Test-Path $_})]
[string]$Path = "."
)
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
#create the output
<#
Thanks to Chris Warwick for the tip on a better way to insert a typename. This must have
been introduced in some previous version of PowerShell and I missed it. Definitely
a cleaner solution.
#>
$obj = [PSCustomObject]@{
PSTypeName = "gitSize"
Path = $full
Files = $stat.count
Size = $stat.sum
} #customobject
<# this is the 'old-school' way to insert a new typename or if you can't define it at runtime
$obj.psobject.typenames.insert(0, "gitSize")
#>
<#
write object to the pipeline. As currently written I didn't have to save the custom object
to a variable. I would only need to do that if I were going to do something else with it
before writing it to the pipeline.
#>
$obj
} #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
}
#add custom properties
#these properties could also be added to the original object
Update-TypeData -TypeName gitSize -MemberType ScriptProperty -MemberName SizeKB -Value {$this.size / 1kb} -force
Update-TypeData -TypeName gitSize -MemberType ScriptProperty -MemberName SizeMB -Value {$this.size / 1mb} -force
Update-TypeData -TypeName gitSize -MemberType ScriptProperty -MemberName SizeGB -Value {$this.size / 1gb} -force
Update-TypeData -TypeName gitSize -MemberType NoteProperty -MemberName Computername -Value $env:COMPUTERNAME -force
Update-TypeData -TypeName gitSize -MemberType ScriptProperty -MemberName Date -Value {Get-Date} -force
Update-TypeData -TypeName gitSize -MemberType ScriptProperty -MemberName Name -Value {Split-Path -path $this.path -leaf} -force
#set default properties
Update-TypeData -TypeName gitSize -DefaultDisplayPropertySet "Path", "Files", "Size" -Force
@jdhitsolutions
Copy link
Author

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