Last active
November 7, 2017 17:30
-
-
Save bobfrankly/c24bfb3d2143475472a0ea1dd5bb4430 to your computer and use it in GitHub Desktop.
Function to return consumed space in regular and compressed values, for windows deduplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ConsumedSpace | |
{ | |
#.Synopsis | |
# Get folders, and space consumed by each | |
[cmdletbinding()] | |
param( | |
# Target Location | |
[Parameter(Mandatory=$True)] | |
[string]$folder, | |
[switch]$inGB | |
) | |
try | |
{ | |
[Win32Functions.ExtendedFileInfo] | Out-Null | |
} | |
catch | |
{ | |
# Gleefully lifted from https://stackoverflow.com/questions/554010/how-to-get-the-actual-size-on-disk-of-a-file-from-powershell | |
add-type -type @' | |
using System; | |
using System.Runtime.InteropServices; | |
using System.ComponentModel; | |
namespace Win32Functions | |
{ | |
public class ExtendedFileInfo | |
{ | |
[DllImport("kernel32.dll", SetLastError=true, EntryPoint="GetCompressedFileSize")] | |
static extern uint GetCompressedFileSizeAPI(string lpFileName, out uint lpFileSizeHigh); | |
public static ulong GetCompressedFileSize(string filename) | |
{ | |
uint high; | |
uint low; | |
low = GetCompressedFileSizeAPI(filename, out high); | |
int error = Marshal.GetLastWin32Error(); | |
if (high == 0 && low == 0xFFFFFFFF && error != 0) | |
throw new Win32Exception(error); | |
else | |
return ((ulong)high << 32) + low; | |
} | |
} | |
} | |
'@ | |
} | |
function Get-CFS | |
{ | |
Param($path) | |
[Win32Functions.ExtendedFileInfo]::GetCompressedFileSize((Get-Item $path).FullName) | |
} | |
New-PSDrive -Name "space" -PSProvider FileSystem -Root $folder | Out-Null # Dodging Char Limits 101 | |
$filelist = Get-ChildItem -force -recurse space:\ | select Length, @{Name='CompressedLength';e={ [Win32Functions.ExtendedFileInfo]::GetCompressedFileSize($_.FullName) }} | |
$Size = $filelist | Measure-Object -property length -sum | |
$CFSize = $filelist | Measure-Object -Property CompressedLength -sum | |
if ($inGB -eq $false) | |
{ | |
$thisObject = "derp" | Select-Object @{Name="UFSize";e={$Size.sum}},` | |
@{Name="CFSize";e={$CFSize.sum}},` | |
@{Name="Count";e={$size.count}} | |
Return $thisObject | |
} | |
else | |
{ | |
######### Make the size number human readable GB | |
$thisObject = "derp" | Select-Object @{Name="UFSize";e={"{0:N2}" -f ($Size.sum / 1GB)}},` | |
@{Name="CFSize";e={"{0:N2}" -f ($CFSize.sum / 1GB)}},` | |
@{Name="Count";e={$size.count}} | |
Return $thisObject | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment