Repository navigation
Expand file tree
/
Copy pathFileUsageReport.ps1
More file actions
42 lines (34 loc) · 1.47 KB
/
Copy pathFileUsageReport.ps1
File metadata and controls
42 lines (34 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#requires -version 7.4
#requires -module PSScriptTools
#this is a demonstration script
[cmdletbinding()]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Specify a path like C:\Scripts")]
[ValidateScript( { Test-Path $_ })]
[string]$Path,
[switch]$Recurse
)
#create a transcript if -Verbose is used
if ($VerbosePreference -eq 'Continue') {
$transcript = New-CustomFileName -Template "Transcript-$($MyInvocation.MyCommand)-%Year%Month%Day%Time.log"
Start-Transcript -Path $transcript -IncludeInvocationHeader
}
$name = Split-Path -Path $path -Leaf
$file = New-CustomFileName -Template "FolderReport-$($name)_%yr%month%day.log"
$log = Join-Path -Path $env:USERPROFILE\Documents -ChildPath $file
$PSBoundParameters.Add("File", $True)
$files = Get-ChildItem @PSBoundParameters
$grouped = $files | Group-Object -Property {
if (-Not $_.extension) { "N/A" }
else { $_.Extension.Substring(1) }
}
$grouped | Add-Member -MemberType ScriptProperty -Name Size -Value { ($this.group | Measure-Object -Property length -Sum).sum }
Format-BorderBox -Text "Folder Usage: $path" -BorderColor $PSStyle.Formatting.Verbose
$grouped | Sort-Object -Property Name
Set-Content -Path $log -Value "Usage Report for $Path"
Add-Content -Path $log -Value (Get-Date)
$data | Select-Object Count, Name, Size | Out-String | Add-Content -Path $log
if ($Transcript -AND (Test-Path $Transcript)) {
Stop-Transcript
Write-Verbose "See $transcript for a transcript of this script."
}