PowerTip: Create Self-Updating Variable

Doctor Scripto

Dr Scripto

Summary: Learn how to create a self-updating variable.
Hey, Scripting Guy! Question How can I create a variable that will automatically update when I call it?

Hey, Scripting Guy! Answer Use Set-PSBreakPoint to set a variable that will always have the current date and time (or only time):

Set-PSBreakpoint -Variable Time -Mode Read -Action {$Global:Time=(Get-Date).ToShortTimeString();$Global:Time}

$Time

Set-PSBreakpoint -Variable Date -Mode Read -Action {$Global:Date=Get-Date;$Global:Date}

$Date

Note  You need to call the variable to kick off the PSBreakPoint action script block the first time.
           After that, the information will be up-to-date.

PS C:> $Date

Wednesday, October 29, 2014 10:01:28 PM

PS C:> $Time

10:01 PM

Doctor Scripto
Dr Scripto

Scripter, PowerShell, vbScript, BAT, CMD

Follow Dr Scripto   

0 comments

    Leave a comment