PowerShell: How To Create A Reference on Object Properties

Question:

Some Facts:

When you asign an object to a variable called $a and then one of it’s propertys changes, the variable $a gets updated.

But when I asign the value of an object’s property $object.property (instead of the object itself) to the variable called $b and then $object.property changes, $b doesn’t get updated. That means, the current value is stored in $object.property, but $b stays the way it is.

An example:

I asign an Window object to a variable called $bochsWindow. Then some propertys change because I move the window. But when I print out $bochsWindow, you can see that it’s up to date – that means, all new values of the object’s propertys are also stored in $bochsWindow.

But if try to store a property of $bochsWindow in a variable called $posX and then the property changes, $posX doesn’t change.

But what should I do if I want $posX to stay up to date and always store the new value (472) instead of 302

My Question:

I want to store a reference the an object’s property in a variable. That means, I want the variable to get updated every time the object’s property changes. How can I do this? Thanks.

Answer:

Not trivial way using PSBreakpoint, but it’s the only that I know:

In this way $b is always updated when called.

Source:

PowerShell: How To Create A Reference on Object Properties by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply