Timer to turn off automated task - Update

I have an automated folder polling task running which I need to run only for a predetermined time and then turn off while I take the changed contents of the folder and perform a Move-Item command to the same. Can someone help me with the timer portion of the script please?

Update: I have everything working. I just need a way to stop the polling function from running after the script has executed once… PERIOD.

I assume it will be a command at the end of the script as the last line

Thank you

Add-Type -AssemblyName System.Windows.Forms 
[System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”)

Function Register-Watcher {
    param ($folder)
    $filter = "*.*" #all files
    $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
        IncludeSubdirectories = $false
        EnableRaisingEvents = $true
    }

    $changeAction = [scriptblock]::Create('
        # This is the code which will be executed every time a file change is detected
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"
    ')
    

    
    $changeAction2 = [scriptblock]::Create('
        # This is the code which will be executed every time a file change is detected
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"
    ')

    
    $changeAction3 = [scriptblock]::Create('
        # This is the code which will be executed every time a file change is detected
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"
    ')

    
    $changeAction4 = [scriptblock]::Create('
        # This is the code which will be executed every time a file change is detected
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"
    ')  


    Register-ObjectEvent $Watcher -EventName "Changed" -Action $changeAction
    Register-ObjectEvent $Watcher -EventName "Created" -Action $changeAction2
    Register-ObjectEvent $Watcher -EventName "Deleted" -Action $changeAction3
    Register-ObjectEvent $Watcher -EventName "Renamed" -Action $changeAction4
}

 Register-Watcher "c:\temp"

By polling function do you mean unregister the watcher?

https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/using-filesystemwatcher-correctly-part-2

Forgive my amateur use of the term “polling” Rob. Yes you are correct. I want to place a command at the end of the script I shared that causes the polling function to completely CEASE as the code I shared is part of a bigger script. Then the rest of the code will run and at the end of the script I will LOOP the entire script which will allow the code to run once more , then once again stop, until the next run of the script from the loop. Make sense Sir?

The sample code from the link you shared seems to do the job except for one problem. The user STILL has to manually push CNTR + C to execute the unregister sequence.
I need some sample code that will emulate the CNTR + C Keystroke in code without the use for manual input.

Any Ideas?

Does not make sense. The code is “Watch directory C:\Temp for changes and do this when changes are detected”. What would occur that you would like to stop watching the directory? Emulating key presses are a bad practice and most likely unnecessary. When you are doing send keys, it is an absolute last resort and still cannot ensure it’s going to work. There are multiple threads all going around the same solution. Have you provided exactly what you are trying to accomplish? For instance, if you wanted to once an hour process a directory, FileWatcher is not the solution. FileWatcher is basically to continually watch a directory or file and immediately do something, typically as long as the server is up. Filewatcher would be a startup script or task and it would run processing files like a queue.

Ok Rob understood and thank you for that clarification. Having said that if “File Watcher” is NOT the solution can you provide a sample of what would be a viable solution? I only need to scan the folder once per run, keep the scan active for a predetermined period of time and then cease that operation.

Please advise.

Thank you Sir for your valuable input!!!

That is a bit contradictory, either you are processing one time or you want to keep processing for a specified amount of time. Look at this example:

$timeout = 1

do {
    'Executing iteration {0}' -f $timeout

    $files = Get-ChildItem C:\Scripts\*.txt -File | Select -First 3

    foreach ( $file in $files) {
        'Processing file {0}' -f $file.FullName
    }

    Start-Sleep -Seconds 5
    $timeout++

}
while ($timeout -le 5)

This would process the directory 5 times with a 5 second timeout, which would be approx 25 seconds as we are not processing anything. The sleep could be and timeout count would be incremented to adjust how many time the directory is processed in a given time.

Executing iteration 1
Processing file C:\Scripts\amy_Temp1.txt
Processing file C:\Scripts\amy_Temp2.txt
Processing file C:\Scripts\amy_Temp5.txt
Executing iteration 2
Processing file C:\Scripts\amy_Temp1.txt
Processing file C:\Scripts\amy_Temp2.txt
Processing file C:\Scripts\amy_Temp5.txt
Executing iteration 3
Processing file C:\Scripts\amy_Temp1.txt
Processing file C:\Scripts\amy_Temp2.txt
Processing file C:\Scripts\amy_Temp5.txt
Executing iteration 4
Processing file C:\Scripts\amy_Temp1.txt
Processing file C:\Scripts\amy_Temp2.txt
Processing file C:\Scripts\amy_Temp5.txt
Executing iteration 5
Processing file C:\Scripts\amy_Temp1.txt
Processing file C:\Scripts\amy_Temp2.txt
Processing file C:\Scripts\amy_Temp5.txt

Thank you very much and have a blessed day!!!

That script is beautiful Rob. It is exactly what I needed. Thank you so much!!!