PowerShell Visual Studio Project - sharing data across multiple .PS1 files

Tool: Visual Studio, Visual Studio Code, PSScriptPad, PowerShell Module

Hi Everyone, I am trying to create a new Forms UI for a fairly lengthy PowerShell Script. The Script was previously run at the PowerShell Command Line…

When I create a PowerShell Form Solution in Visual Studio 2022, there is an initial PowerShellFormProject1.ps1 for the Forms

How do I add additional .PS1 files and Share information between them and the PowerShellFormProject.ps1 code

Should I define $global: … variables?
Should I declare them in a .psm1 file?

Also, where should I put code that initializes the application prior to the Form coming up and likewise, where should I put the code to deconstruct everything I set up before I exit?


I only use WPF for my PowerShell GUI’s as it looks a lot more modern, but I would imagine its pretty similar. I suppose its personal preference really, I personally like to use modules for all the functions that do the work, this way if you ever want to make some automation pipelines, you can simply re-use the functions in the modules outside of the GUI.
If its nothing to major I would just have all the script for the GUI, in a single script, and when you click buttons it just calls the functions from the module/s.
If you want a fluent GUI that doesn’t freeze, while its doing work in the background, you would need to have the GUI running in a seperate runspace, and use a SyncHash table to move data between the runspaces.