Creating Intelligent Variables

by Oct 31, 2011

To create variables that calculate their content each time they are accessed, use access-triggered variable breakpoints. This line, for example, creates a global variable called $today, and each time you access its content, it returns the current time and date:

$global:today = Set-PSBreakpoint -Variable today -Mode Read -Action { $global:today = Get-Date }

Try it: each time you output $today, it returns the correct date and time – because each time you access it, the associated script block updates its content.

Twitter This Tip!
ReTweet this Tip!