-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-FileSizeOnDisk.ps1
115 lines (98 loc) · 3.48 KB
/
Get-FileSizeOnDisk.ps1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function Get-FileSizeOnDisk {
<#
.SYNOPSIS
Powershell script to get file size and size on disk of all files in a directory.
.DESCRIPTION
This PowerShell script gets file size and size on disk in bytes of all files in a directory.
.PARAMETER Path
Directory path of the files to check. If this parameter is not specified the default value is current directory.
.PARAMETER Full
Switch to return all attributes of Get-Item on the file plus the attribute SizeOnDisk
.NOTES
Inspired by: https://www.opentechguides.com/how-to/article/powershell/133/size-on-disk-ps.html
Changes:
* CmdletBinding
* write-verbose
* more help
* added -Full switch to get full attributes of a file including SizeOnDisk
* added ability to accept input from the pipeline
.LINK
www.opentechguides.com
.EXAMPLE
Get-FileSizeOnDisk c:\myfolder
.EXAMPLE
Get-FileSizeOnDisk -Path *.psd1
Name Length SizeOnDisk FullName
---- ------ ---------- --------
PoshFunctions.psd1 21190 12288 C:\Git\PoshFunctions\PoshFunctions.psd1
.EXAMPLE
Get-FileSizeOnDisk -Path *.psd1 -Full | Select-Object Name, Length, SizeOnDisk, LastWriteTime
Name Length SizeOnDisk LastWriteTime
---- ------ ---------- -------------
PoshFunctions.psd1 21190 12288 11/22/2022 1:35:42 PM
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
[CmdletBinding()]
[OutputType([psobject[]])]
param (
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[string[]] $Path = '.',
[switch] $Full
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
$source = @'
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.IO;
namespace Win32
{
public class Disk {
[DllImport("kernel32.dll")]
static extern uint GetCompressedFileSizeW([In, MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
[Out, MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);
public static ulong GetSizeOnDisk(string filename)
{
uint HighOrderSize;
uint LowOrderSize;
ulong size;
FileInfo file = new FileInfo(filename);
LowOrderSize = GetCompressedFileSizeW(file.FullName, out HighOrderSize);
if (HighOrderSize == 0 && LowOrderSize == 0xffffffff)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
else {
size = ((ulong)HighOrderSize << 32) + LowOrderSize;
return size;
}
}
}
}
'@
Add-Type -TypeDefinition $source
}
process {
foreach ($CurPath in $Path) {
Get-ChildItem -Path $CurPath | Where-Object { -not $_.PSIsContainer } | ForEach-Object {
$Item = $_
[int64] $size = [Win32.Disk]::GetSizeOnDisk($item.FullName)
$Item | Add-Member -MemberType NoteProperty -Name SizeOnDisk -Value $size
if ($Full) {
$Item
} else {
New-Object -TypeName psobject -Property ([ordered] @{
Name = $item.Name
Length = $item.Length
SizeOnDisk = $item.SizeOnDisk
FullName = $item.FullName
})
}
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}