PowerShell Watch function

0

A function to execute a scriptblock in a loop with a certain interval (seconds)

function Watch {
    param(
        $Interval,
        $ScriptBlock
    )

    While (1 -eq 1 ) {
        Clear-Host
        & $ScriptBlock 
        Start-Sleep $Interval
    }
}

$ScriptBlock = { Get-ChildItem 'c:\temp' | Select Mode, LastWriteTime, Length, Name | Format-Table -AutoSize -HideTableHeaders }

Watch -Interval 5 -ScriptBlock $ScriptBlock
Share.

About Author

Christian Unnerstall

Christian Unnerstall, founder of devshooin.com, enjoys designing and building things that look nice and work well, whether it's websites or services within the interwebs. Have questions - feel free to reach out to him. Enjoy

Leave a Reply