PowerShell Watch Script Command

Here is a simple PowerShell script to get something similar to the watch command on line.  Just put it in your PowerShell modules directory and run “Import-Module Watch”

# Author:       John Rizzo
# Created:      06/12/2014
# Last Updated: 06/12/2014
# Example:      Watch 5 Get-Date

function Watch {
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
    param (
        [Parameter(Mandatory=$False,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [int]$interval = 10,

        [Parameter(Mandatory=$True,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [string]$command
    )
    process {
        $cmd = [scriptblock]::Create($command);
        While($True) {
            cls;
            Write-Host "Command: " $command;
            $cmd.Invoke();
            sleep $interval;
        }
    }
}

Export-ModuleMember -function Watch

John Rizzo

Technology, problem solving, learning, business, society and where those things intersect is what I am always thinking about. From my hobbies to my profession I attempt to combine those interests in a way that makes the sum greater than the whole. Find out more about me at linkedin, http://www.linkedin.com/in/johnrizzo1.

You may also like...