Creating Self-Updatable Variables

by Aug 22, 2011

If you want a variable to update its content every time you retrieve it, you can assign a breakpoint and an action to it. The action script block will then be executed each time the variable is read:

$Global:Now = Set-PSBreakpoint -Variable Now -Mode Read -Action {  Set-Variable Now (Get-Date)
-Option ReadOnly, AllScope -Scope Global -Force }

So now, each time you output $now, it will return the current date and time. Thanks to Robert Robelo for pointing out this one.

Twitter This Tip!
ReTweet this Tip!