-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathGet-FileSizeOnDisk.ps1
85 lines (66 loc) · 2.2 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
# https://www.opentechguides.com/how-to/article/powershell/133/size-on-disk-ps.html
<#
.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.
.NOTES
Version: 1.0
Author: Open Tech Guides
Creation Date: 06-Feb-2017
.LINK
www.opentechguides.com
.EXAMPLE
Get-FileSizeOnDisk c:\myfolder
#>
function Get-FileSizeOnDisk {
param (
[string]$path = '.',
[switch]$recurse
)
$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
if ($recurse) {
Get-ChildItem $path -Recurse | Where-Object { ! $_.PSIsContainer} | Foreach-Object {
$_ | select fullname, name, length, extension, basename, @{n='SizeOnDisk';e={[Win32.Disk]::GetSizeOnDisk($_.FullName)}}
}
} else {
Get-ChildItem $path | Where-Object { ! $_.PSIsContainer} | Foreach-Object {
$_ | select fullname, name, length, extension, basename, @{n='SizeOnDisk';e={[Win32.Disk]::GetSizeOnDisk($_.FullName)}}
}
}
}