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