I've a script file containing a class definition. Using the new V5 Powershell syntax. I've another script file that dot source the class and uses it. I'm testing that file in the console.
On clean startup of the console it will load the class correctly. I then make some changes to the class and saved them back to file. Re-running the script does not appear to pick the changes up. It is as though once loaded into the console, the class is immutable and will not be parsed and reloaded.
This really hurt when developing, since I have to spin up a new console after any change to a class.
Is there a way to force Powershell to reload classes each time it sees them?
This occur in code debugged both in Powershell ISE and in the Powershell console (using vscode).
But, to import the classes defined in a module into your session, you can't use Import-Module or even #Requires -Module. Instead, use the Using statement, introduced in PowerShell 5.0, which imports the module, including its classes, into the caller's session.
Tip: You can reload a module by using the –Force parameter. It can be useful during development when you updated your module and you want to see your latest changes.
PowerShell 5.0 adds a formal syntax to define classes and other user-defined types. The addition of classes enables developers and IT professionals to embrace PowerShell for a wider range of use cases.
PowerShell is an object-oriented scripting language; however, moderately complex scripts are often written using a procedural/functional approach.
This is a known issue with classes in Powershell
, and looks like the issue remains unresolved at this time of writing.
To workaround this today, you either need to start a new interactive session (which you don't want to do), or load your development class in a child instance of Powershell:
powershell.exe -Command { Your-Code }
# Alternatively, write a script to test your newly updated class
powershell.exe -File \path\to\Test-ClassScript.ps1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With