December 5th, 2014

PowerTip: Create Self-Updating Variable

Doctor Scripto
Scripter

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

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.