PowerShell 5 and classes - Cannot convert the "X" value of type "X" to type "X" PowerShell 5 and classes - Cannot convert the "X" value of type "X" to type "X" powershell powershell

PowerShell 5 and classes - Cannot convert the "X" value of type "X" to type "X"


I was able to replicate and resolve your issue. It is similar to if you define a class in a scope and you try to define another class in the same scope. The class definition is retained in PowerShell session. In Script1.ps1 you need to modify the code to not explicitly declare the variable to type. Just use as below to not use Strong Typing and let PowerShell determine the type and assign it dynamically:

. $PSScriptRoot\Common.ps1$v = New-Object X

Now you should be able to change the definition of class X in Common.ps1 as many times as you want without needing to close and reload.

The example above uses "Weak typing" You can read more about this and other details here: Variable Types and Strongly Typing