Why can’t I use predefined variables in class methods?

Question:

I tried to use predefined variables like $PSVersionTable or $PSScriptRoot in a class method. They failed with the error message

Variable is not assigned in the method.

Example:

But why?

Answer:


For the “why” part, I guess this is related to scope. In your class you have to specify in some way that you refer to the global $PSVersionTable variable, and not to something in the class or script scope.

Source:

Why can’t I use predefined variables in class methods? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply