Repeat foreach loop after all iteration completed in Powershell RRS feed

  • Question

  • Anyone here can help me how to repeat the code from the beggining after all iteration in foreach loop has been completed. The code below will get all the files having 'qwerty' pattern inside the file, feed the list in foreach loop and display the filename and last 10 lines on each file and terminate the code if there is no new/updated file in certain amount of time

    $FILES=Get-ChildItem -Path C:\Test\ | `
        Where-Object {$_.LastWriteTime -ge $today} | `
        Select-String -pattern "qwerty" | `
        Select-Object FileName -Unique
    foreach ($i in $FILES) {
       Write-host $i -foregroundcolor red
       Get-content -Path \\XXXXXX\$i -tail 10
       Start-Sleep 1

    • Edited by Mr. Scayod Sunday, December 29, 2019 8:14 PM
    Sunday, December 29, 2019 12:53 PM

All replies

  • You forgot to post your code. Please format your code as code: How to Use the Code Feature in a TechNet Forum Post

    Live long and prosper!


    Sunday, December 29, 2019 1:53 PM
  • Get-ChildItem C:\Test -File |
        Where-Object {
            $_.LastWriteTime -ge [datetime]::Today -and ($_|Select-String -pattern 'qwerty')
        } |
            Write-host $_.Fullname -foregroundcolor red
            Get-content $_.Fullname -tail 10
            Start-Sleep 1
    Don't use line extenders.  They will cause you much grief.


    Sunday, December 29, 2019 9:25 PM
  • To repeat the code just place it in a loop.

    To get only new files use the "FileSystemWatcher"


    Sunday, December 29, 2019 9:26 PM