I essentially want to create a variable that would be executed every time. For the simplest example:
$myvar = `write-host foo`;
Then everytime I referenced $myvar, it would output foo:
dir $myvar
Directory of foo:
The point being that the write-host foo portion would be re-executed everytime I reference $myvar
It’s doable in managed code (C#/VB) by creating your own PSVariable derived class, but not directly in pure script, sorry. I say “pure script” because in powershell v2 you could inline the C# with add-type. That said, you could hack it in script by relying on implicit ToString calls but this would not be reliable in every situation. Example:
Note the tick count is different on each evaluation of the variable. If of course if you just use a regular function instead of a variable, this is much easier.
Hope this helps
-Oisin