Getting a file move function to work in a PowerShell script

Hello!

I was able to work up this little PowerShell script in ChatGPT. My knowledge in PowerShell is very limited. Basically this is a small utility that watches a folder for new images. When the image shows up, the script calls on AutoHotKey to run some image processing through a desktop app. When the image is processed, it is supposed to move the image to the ‘Processed’ folder. For my life of me I cannot get the file move function to work. Everything else in the code seems to work, just not the move.

Any ideas?

$folder = 'C:\Users\***********\MJbotWatcher'  
$filter = '*.png' 

# Processed directory path
$processedPath = "C:\Users\*********\Processed"

# Check if Processed directory exists, if not, create it
if (-not (Test-Path $processedPath)) {
    New-Item -ItemType Directory -Path $processedPath
}

$watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $false
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

$action = {
    $path = $Event.SourceEventArgs.FullPath
    Write-Host "Starting the action for file: $path"
    Write-Host "File '$path' was added!"
    Add-Content -Path "C:\Users\*****************\queue.txt" -Value $path
    
    # Call the AHK script and pass the image's path as an argument
    #Start-Process -FilePath "C:\Users\**************\Automation.ahk" -ArgumentList $path  # Ensure you provide the correct path to the AHK script here
    #Start-Sleep -Seconds 10  # Adjust this based on how long it typically takes
   
    # Try moving the file with retries
    $retries = 3
    $errorOccurred = $false

    while ($retries -gt 0) {
        try {
            move $path $processedPath -Force
            $retries = 0  # Successfully moved the file
        } catch {
            # An error occurred, so wait and retry
            Start-Sleep -Seconds 5
            $retries--
            if ($retries -eq 0) {
                Write-Host "Failed to move file after multiple attempts."
            }
        }
    }
    
    if (-not $errorOccurred) {
        Write-Host "File $path moved to $processedPath"
    }
    

    Write-Host "File $path moved to $processedPath"
}

$created = Register-ObjectEvent $watcher Created -Action $action

while ($true) { sleep 5 }

Any errors thrown? I didn’t run the full code but ran enough to believe the above should work. The AHK script saves the item back out to the same path, after it processes it correct? It doesn’t change the name or the extension. Note that the code above would also force an overwrite (-force switch) of existing file with the same name I believe.

I tested the code (basically modified it so i didn’t need to rely on the AHK part) and it works for me. I create a file in folder TestA, and it immediately moves it to a folder I named TestB.