DEV Community

Discussion on: How I Set Up an Awesome PowerShell Environment for Script Development

Collapse
 
kovesp profile image
Peter Koves • • Edited

IMHO, it is better to have 'using module ModuleName' for the modules used in a script. I want to know the dependencies of each script. As well, using takes effect at compilation time, wheras Import-Module happens at run-time.

For 'using module ...' to work seamlessly (using your environment as an example) you need to

  1. Have Utilities and Business on PSModulePath
  2. Each module must be in its own directory under either Utilities or Business.
    • The directory must have the same name as the PSM1 file.
    • The directory must contain the PSM1 file as well as the corresponding PSD1 file.
    • The PSD1 file can be generated by the New-ModuleManifest cmdlet.
    • At the minimum, set the RootModuleentry to the name of the PSM1 file.
    • Each function must be listed in the FunctionsToExportentry. The other ...ToExport entries should be self-explanatory. Classes do not need an entry.

If you insist on importing everything from the profile you can use

foreach ($path in @('Utilites','Business') {
   foreach ($mod in Get-ChildItem $path -Directory) {
      Invoke-Expression "using module $($mod.Name)"
   }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mortiel profile image
Mortiel • • Edited

There is upsides and downsides to "using module..." that I've found.

Mainly, "using module" is absolutely required if you use custom classes in a script module, as custom classes do not load with Import-Module.

However, if you have to script using Windows PowerShell 5.1 (example: scripting for Windows Server with security restrictions), I've encountered bugs with "using module" that were fixed in later PowerShell Core versions.

I suppose that's an argument for creating compiled DLL modules, but I digress.

The main takeaway is that you should be very selective with the custom modules you load using the PSProfile as opposed to in the actual scripts, but instead only adding custom modules directories to PSModulePath is generally good to make "using" or "Import-Module" not require a full directory path.

Collapse
 
kovesp profile image
Peter Koves •

Agreed. I was using Import-Module until I placed my first class in a module and spent a few hours trying to understand why my class was not being seen.

The fact that having using module in a script will not reload the modified class when running the script in a session still bites. If only functions/cmdlets are modified in the module, one can use Import-Module -Force (ipmo -fo) to reload the module. But if a class is changed, a new session is needed. Which is a pain in VS Code: 3 clicks to restart the session + losing variables set in it.

The only alternative is to select the class and use F8. But selecting a class isn't quite straight forward in VS Code; at best it involves using Expand-Selection (Alt-Shift-RightArrow) multiple times.

Thread Thread
 
mortiel profile image
Mortiel •

Using VS Code, you can pin the PowerShell menu to the status bar, which reduces the number of clicks needed down to 2, but better than using ISE, I suppose.

The other method is writing the module in .NET/C# as a DLL, which allows creating namespaces and more class capabilities. Sadly, I'm not extremely knowledgeable with C# and haven't really needed to go that route yet.