Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to choose development of a PowerShell Module over PowerShell Script

I am about to write a PowerShell Script for Windows administrators, in order to help them in certain tasks related to deployment of a web application.

Is there any reason I should favor or exclude the development of a PowerShell Module (.psm1) instead of doing a PowerShell script (.ps1)?

Arguments to develop a Script

  • simplicity: I think that using a script is a bit easier and more straightforward for Windows Administrators as it does not require the module to be installed (but I might be wrong as I am not a Windows Admin!).
  • faster development: developing a module requires more careful programming an exposure to internal methods. It is like designing an API an thus must be more rigorous.

Arguments to develop a Module:

  • reusability: this is the first thing that comes to mind: if the administrator wants to integrate our script in his own script, it might be easier for him to reuse a module exposing one (or several) cmdlet rather than invoke our script?
  • ...

If you know the common use case of PS scripts vs PS modules, or the technical limitations of each choice, it might help.

like image 837
Olivier Avatar asked Feb 24 '11 10:02

Olivier


People also ask

What is the difference between a PowerShell script and a PowerShell module?

At heart, a script module is simply a Windows PowerShell script with a different extension, which allows administrators to use import, export, and management functions on it.

Is PowerShell good for development?

Learning PowerShell can be very useful for people with programming experience. Whether you're currently a developer, dev-ops, or an admin you can get some serious use out of being familiar with PowerShell.

Why is PowerShell an ideal platform for script development?

PowerShell Is Powerful and VersatileNET, which means it can call . NET methods natively to accomplish tasks — even if the built-in PowerShell cmdlet doesn't support it. On top of that, PowerShell can also run anything that you can already run from the command prompt.


2 Answers

To understand what modules can do for you, read this: https://docs.microsoft.com/en-us/powershell/scripting/developer/module/writing-a-windows-powershell-module?view=powershell-7.1

In a nutshell,

Windows PowerShell modules allow you to partition, organize, and abstract your Windows PowerShell code into self-contained, reusable units. With these reusable units, administrators, script developers, and cmdlet developers can easily share their modules directly with others. Script developers can also repackage third-party modules to create custom script-based applications. Modules, similar to modules in other scripting languages such as Perl and Python, enable production-ready scripting solutions that use reusable, redistributable components, with the added benefit of enabling you to repackage and abstract multiple components to create custom solutions.

If your script already has functions and is not just written to perform a single task, you can just rename it to .PSM1 to convert it to module. If you are not using functions, of course, there is no choice but to go for .ps1. In such a case, each .ps1 will be used to perform a single task. I always prefer modules when sharing the scripts I write with others.

like image 159
ravikanth Avatar answered Sep 24 '22 19:09

ravikanth


I like modules for the ability to "hide" functions/variables and only export the ones that I want.

like image 30
Mike Shepard Avatar answered Sep 23 '22 19:09

Mike Shepard