Powershell to move files based on size

Hey guys,
I’m trying to move pdf’s that are below or above a certain size to their appropriate folders. Here’s what I have so far, for some reason runs and deletes the source file, but doens’t move it and I don’t see any errors.

#----File Size---#
$Filesize=4KB

#----Define folders to use----#
$SourceFolder = "source folder path"
$AutoFolder = "good folder"
$TooSmallFolder = "toosmall folder"

#----Define extension----#
$Extension = "*.pdf*"

#----Get files that are too small and move them----#
$Files = Get-Childitem $SourceFolder -Include $Extension -Recurse | Where { $_.Length -lt $Filesize} 
 
foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        write-host "Moving File $File" -ForegroundColor "Red"
        Move-Item -Destination '$TooSmallFolder''$File' -force 
        }
    else
        {
        Write-Host "No more small files to move!" -foregroundcolor "Green"
        }
    }
#----Get files that are good and move them----#

$Files = Get-Childitem $SourceFolder -Include $Extension -Recurse | Where { $_.Length -gt $Filesize} 
 
foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        write-host "Moving File $File" -ForegroundColor "Red"
        Move-Item -Destination '$AutoFolder''$File' -force 
        }
    else
        {
        Write-Host "No more good files to move!" -foregroundcolor "Green"
        }
    }

Joe,
Welcome to the forum. :wave:t4:

I think you’re overcomplicating this. This should be all you need:

$Filesize = 4KB
$Extension = '*.pdf'
$AutoFolder = 'good folder'
$TooSmallFolder = 'toosmall folder'

Get-Childitem -Path $SourceFolder -Filter $Extension -Recurse -File |
    ForEach-Object{
        if ($_.Length -lt $Filesize) {
            Move-Item -Path $_.FullName -Destination $TooSmallFolder
        }
        Else{
            Move-Item -Path $_.FullName -Destination $AutoFolder
        }
    }

Of course the desination folders should exist already before you run this code. :wink:

BTW: When you post code please format it as code using the preformatted text button ( </> ) . Simply place the cursor on an empty line, click the button and paste your ocde.

Thanks in advance.

1 Like

Ahh Perfect, I did end up simplifying it a bit as well, but yours looks much cleaner.

Thank you!

Any idea why it’s not outputting the file name to my log?

ForEach-Object{
        if ($_.Length -lt $Filesize) {
            Move-Item -Path $_.FullName -Destination $TooSmallFolder
            logit -----'File is too small - moved to TooSmall Folder' 
            logit $File 
        }
        Else{
            Move-Item -Path $_.FullName -Destination $AutoFolder
            logit -----'File is good - moved to Auto Folder' 
            logit $File 
        }

Would it be enough when I say “yes”? :wink:

Let me answer with a question: Where did you define the variable $File?

Ahh, we don’t have a $FILE variable anymore with your code. How would we grab it with your get-item loop? (for each file).

I thought it would have been obvious. :wink: What is it what represents the current element in a pipeline?

Hint: It is the variable you use to tell Move-Item what to to move. :wink: :wink:

1 Like

Ohh, it’s just the “$_”

lol thanks buddy

Right. :+1:t4: :wink: The complete object would be $_ … if you only want the Name of the file it would be $_.Name, only the the base name would be $_.BaseName, only the extension would be $_.Extension only the folder would be $_.Directory and so on … :wink:

Perfect, and if I wanted to I could change $_ to “$File” or something right?

Hmmm … I don’t know what exactly you mean with that. The pipeline variable is an automatic variable and is always either $_ or $PSItem. These are reserved names you cannot change them.

Oh I see, which is how we get by without defining a variable for each file like I did in my original script.

Hmmm yes and no. You may read more about here:

The first cmdlet is a loop using the pipeline and the pipeline variable. And the second one is loop where you define the loop variable by yourself.

1 Like

Thank you for all the info:

This helped me understand a bit better too.