How to check item size and move them if they are over a certain threshold in Powershell

Viewed 428

I'm currently working on a Powershell script that is supposed to do the following: Check all items in a folder, if there are any items over a certain size (let's say 10MB) create a folder (named "toobig") and move those items there.

Here is my script so far:

function delbig {

param (
[parameter (Mandatory=$true)]
 $p
)


$a= Get-ChildItem $p | Where-Object {$_.Length -gt 10000000} | Measure- Object
$a.count


if ($a -gt 0){

    mkdir "$p\tooBig"

}

"$([int]$a)"

}
delbig

I've figured out how to move the items and how to create the folder but my if condition to decide if the actions should be triggered is giving me the following error:

Cannot compare "Microsoft.PowerShell.Commands.GenericMeasureInfo" because it is not IComparable.
At C:\Powertest\movbig.ps1:14 char:1
+ if ($a -gt 0){
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NotIcomparable

Cannot convert the "Microsoft.PowerShell.Commands.GenericMeasureInfo" value of type "Microsoft.PowerShell.Commands.GenericMeasureInfo" to type "System.Int32".
At C:\Powertest\movbig.ps1:20 char:4
+ "$([int]$a)"
+    ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException

So the value in $a should be an int right? And my if condition is supposed to look if the value is greater than 0 (I've tried it with "0" as well).

Any help would be greatly appreciated!

Regards, Gerfi

3 Answers

$a is an instance of a GenericMeasureInfo type, which can't be compared to zero (an int). Use the Count property of $a to compare to zero:

if ($a.Count -gt 0){
    mkdir "$p\tooBig" 
}

Also, I noticed there is a space in Measure- Object which needs to be removed. I'm guessing that's just a typo in the post.

This should do the job:

function Move-BigFiles {
    param([parameter(Mandatory=$true)]$Path)

    $tooBigFiles = Get-ChildItem $Path | Where-Object {$_.Length -gt 10MB}
    if ($tooBigFiles) {
        $dest = mkdir "$Path\TooBig" -Force
        $tooBigFiles | Move-Item -Destination $dest
    }
}

PowerShell conditions can use 'truthy' values. Any nonempty collection is interpreted as $true.

The problem with your script is type mismatch. $a is object of type Microsoft.PowerShell.Commands.GenericMeasureInfo, as a result of Measure-Object call. It can't be compared to int value.

Thank you both very much, with your help I now got the script to do what I want it to:

function delbig {

param (
[parameter (Mandatory=$true)]
 $p
)


$toobigfiles= Get-ChildItem $p | Where-Object {$_.Length -gt 10000000} | Measure-Object
$toobigfiles.count


if ($toobigfiles.Count -gt "0"){

    mkdir "$p\tooBig"
}


$toobigfiles= Get-ChildItem $p | Where-Object {$_.Length -gt 10000000}

if ($toobigfiles.Count -gt "0"){

    $toobigfiles | Move-Item -Destination "$p\tooBig"
}

#"$toobigfiles"


}
delbig

This is the first time I wrote a script largely on my own and I'm very happy :) (Started learning Powershell about a week ago and have never written a line of any code in my life)

Related