Getting the 'cluster size' of your hard disk?

M

Marco Shaw

Is there an easy way to get the cluster size setting on a hard disk?

Couldn't seem to find anything in WMI or in any of the .NET classes...

Marco
 

My Computer

H

Hal Rottenberg

On Jul 13, 11:00 pm, Marco Shaw <[email protected]_NO_SPAM_gmail.com> wrote:
> Is there an easy way to get the cluster size setting on a hard disk?


Hmm.... I see a few references to win32_Volume, but the class doesn't
exist on my system.
 

My Computer

M

Matthias Tacke

Marco Shaw wrote:
> Is there an easy way to get the cluster size setting on a hard disk?
>
> Couldn't seem to find anything in WMI or in any of the .NET classes...
>

Hi Marco,

the hard disk has only a sector size.
Cluster size is a matter of formatting.

Try this:
fsutil fsinfo ntfsinfo c:

HTH

--
Greetings
Matthias
 

My Computer

D

Dean Wells \(MVP\)

FWIW, I'm able to get at that info. through WMIC which would almost
certainly indicate it's available through WMI directly -

wmic partition get blocksize

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Marco Shaw" <[email protected]_NO_SPAM_gmail.com> wrote in message
news:[email protected]
> Is there an easy way to get the cluster size setting on a hard disk?
>
> Couldn't seem to find anything in WMI or in any of the .NET classes...
>
> Marco
 

My Computer

M

Matthias Tacke

Dean Wells (MVP) wrote:
> FWIW, I'm able to get at that info. through WMIC which would almost
> certainly indicate it's available through WMI directly -
>
> wmic partition get blocksize
>

Set seems to return the sector size (512 bytes), not the cluster size which
is by default 4096 bytes on ntfs formatted drives.

--
Greetings
Matthias
 

My Computer

D

Dean Wells \(MVP\)

Nod, ... looks like you're right. My apologies, I didn't examine the
results closely enough (FWIW - a bad use of terminology in my opinion;
to me, block is a logical partitioning unit ... c'est la vie.)

Thanks Matthias.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Matthias Tacke" <[email protected]> wrote in message
news:[email protected]
> Dean Wells (MVP) wrote:
>> FWIW, I'm able to get at that info. through WMIC which would almost
>> certainly indicate it's available through WMI directly -
>>
>> wmic partition get blocksize
>>

> Set seems to return the sector size (512 bytes), not the cluster size
> which is by default 4096 bytes on ntfs formatted drives.
>
> --
> Greetings
> Matthias
 

My Computer

K

Kiron

For those of us that don't have access to win32_Volume.
This is, by no means, a lighting fast solution.

WARNING: Depending on the drive's size, this function takes up to five
minutes per drive, because it reads chkdsk.exe's output to determine the
cluster size. The size is shown in bytes.

function Get-ClusterSize ([char[]]$driveLetter = 'c')
{
$clusterSize = @{}
foreach ($drive in $driveLetter)
{
$drive = $($drive + ':').toUpper()
if (test-path $drive)
{
chkdsk $drive > outPut
$line = (select-string each outPut).line
remove-item outPut
[regex]$rx = '\d+'
$size = $rx.Match($line).value
$drive = $drive -replace ':'
$clusterSize.$drive = $size
}
else {"Drive $drive was not found"}
}
$clusterSize | format-table -auto -hide
}

Get-ClusterSize c, d

C 4096
D 4096

If the function is dot sourced you can access a drive's cluster size like:

$clusterSize.d
4096

--
Kiron
 

My Computer

K

Kiron

Adjusted with Matthias' solution for NTFS drives only:
Lighting fast.

function Get-ClusterSize ([char[]]$driveLetter = 'c')
{
$clusterSize = @{}
foreach ($drive in $driveLetter)
{
$drive = $($drive + ':').toUpper()
if (test-path $drive)
{
fsutil fsinfo ntfsinfo $drive > outPut
$line = (select-string 'Bytes Per Cluster' outPut).line
remove-item outPut
[regex]$rx = '\d+'
$size = $rx.Match($line).value
$drive = $drive -replace ':'
$clusterSize.$drive = $size
}
else {"Drive $drive was not found"}
}
$clusterSize | format-table -auto -hide
}

Get-ClusterSize c, d

C 4096
D 4096

If the function is dot sourced you can access a drive's cluster size like:

$clusterSize.d
4096

--
Kiron
 

My Computer

M

Matthias Tacke

Kiron wrote:
> Adjusted with Matthias' solution for NTFS drives only:
> Lighting fast.
>
> function Get-ClusterSize ([char[]]$driveLetter = 'c')
> {
> $clusterSize = @{}
> foreach ($drive in $driveLetter)
> {
> $drive = $($drive + ':').toUpper()
> if (test-path $drive)
> {
> fsutil fsinfo ntfsinfo $drive > outPut
> $line = (select-string 'Bytes Per Cluster' outPut).line
> remove-item outPut
> [regex]$rx = '\d+'
> $size = $rx.Match($line).value
> $drive = $drive -replace ':'
> $clusterSize.$drive = $size
> }
> else {"Drive $drive was not found"}
> }
> $clusterSize | format-table -auto -hide
> }
>
> Get-ClusterSize c, d
>
> C 4096
> D 4096
>
> If the function is dot sourced you can access a drive's cluster size like:
>
> $clusterSize.d
> 4096
>


Hi Kiron,
the output of fsutil is locale dependent. My German version has
'Bytes pro Cluster :' so:
$line = (select-string 'Byte.*Cluster' outPut).line
should be a bit more universal albeit not perfect.

--
Greetings
Matthias
 

My Computer

K

Kiron

Thanks for pointing that out, and for your solution. The good thing is that
the script is easy to adjust depending on the locale.

Danke :)


--
Kiron
 

My Computer

On Jul 14, 8:55 pm, "Kiron" <[email protected]> wrote:
> Thanks for pointing that out, and for your solution. The good thing is that
> the script is easy to adjust depending on the locale.
>
> Danke :)
>
> --
> Kiron


Nice, thank you. The intermediary file and select-string can be
avoided with a small tweak, I think:

$fs = fsutil fsinfo ntfsinfo $drive
$reg = [regex]'(?<=Bytes.*Cluster.*)\d+'
$size = ($fs | %{$reg.matches($_)}).value

or:

$reg = '(?<=Bytes.*Cluster.*)\d+'
$fs = (fsutil fsinfo ntfsinfo $drive) | ?{$_ -match $reg}
$size = $matches[0]

That seems to work fine on my end, anyway.

-Hecks
 

My Computer

K

Kiron

Works here too, they both do, although the first one is slightly faster than
the second one.
Good performance enhancement, less lines and it's faster than the original.
Thanks for sharing the improvement, they're always welcome, corrections too.

--
Kiron
 

My Computer

On Jul 15, 4:57 am, "Kiron" <[email protected]> wrote:
> Works here too, they both do, although the first one is slightly faster than
> the second one.
> Good performance enhancement, less lines and it's faster than the original.
> Thanks for sharing the improvement, they're always welcome, corrections too.
>
> --
> Kiron


Glad to help. I've been regexing a lot recently, so it's always
interesting to see the different ways it can be optimized in PS. :)

This is perhaps a clearer way of writing it, though it doesn't seem to
be any faster:

# outside the foreach loop
$reg = [regex]'(?<=Bytes.*Cluster.*)\d+'

# inside the foreach loop
$fs = fsutil fsinfo ntfsinfo $drive
$size = $reg.match($fs).value

There are just so many ways of doing the same thing in PS, it's
insane ...

-Hecks
 

My Computer

K

Kiron

PowerShell is very versatile.
Here's another way, shorter and a bit faster. This time it gets the
fifteenth line of fsutil's output --considering locale, thanks Matthias--
and removes any nondigit from it to get the cluster's size. Also no
unnecessary repetitive declaration inside the foreach loop --thanks Hecks.
It also relies on the uniformity of fsutil's output in different
languages --of which I know nothing about.


function Get-ClusterSize ([char[]]$driveLetter = 'c')
{
$clusterSize = @{}
foreach ($drive in $driveLetter)
{
$drive = $($drive + ':').toUpper()
if (test-path $drive)
{
$size = (fsutil fsinfo ntfsinfo $drive)[14] -replace '\D'
$drive = $drive[0]
$clusterSize.$drive = $size
}
else {"Drive $drive was not found"}
}
$clusterSize | format-table -auto -hide
}


--
Kiron
 

My Computer

J

Jacques Barathon [MS]

"Kiron" <[email protected]> wrote in message news:[email protected]
> PowerShell is very versatile.
> Here's another way, shorter and a bit faster. This time it gets the
> fifteenth line of fsutil's output --considering locale, thanks Matthias--
> and removes any nondigit from it to get the cluster's size. Also no
> unnecessary repetitive declaration inside the foreach loop --thanks Hecks.
> It also relies on the uniformity of fsutil's output in different
> languages --of which I know nothing about.

....

Here's a sligthly shorter version which has the advantage in my eyes of providing raw data thus allowing for further handling down the pipeline (e.g. formatting or measuring):

--- get-clustersize.ps1 ---
param ([char[]]$drive = "c")

$drive | foreach {
if (test-path "$($_):") {
$cs = new-object PSObject
$cs | add-member NoteProperty Drive $_
$cs | add-member NoteProperty Size (fsutil fsinfo ntfsinfo "$($_):")[14].split()[-1]
$cs
}
}
--- end of file ---

Jacques
 

My Computer

Top