How To Create Portable Modules Using PowerShell In MacOS

Introduction

 
With the introduction of PowerShell 7 it is very easy to use PowerShell across all the platforms and so we will want to create our own custom PowerShell modules as well.
To do so it is very easy with the introduction of .net core --  truly Microsoft is being platform agnostic in terms of development.
 
So let us understand how we can create our own custom module for PowerShell.
 
Before starting let us install PowerShell in macOS.
 

Install PowerShell in macOS

 
Step 1 Install Homebrew
 
To run PowerShell we needto have Homebrew. It is a package manager which is used for installing packages which are missing in macOS.
 
Use the below command in the terminal to install Homebrew.
 
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
 
How To Create Portable Modules Using PowerShell In MacOS 
 
Step 2 Install PowerShell
 
Once we have installed Homebrew now we are good to install PowerShell. This is also recommended by Microsoft ;for more details check the link.
 
Use the below command to install PowerShell.
 
brew cask install powershell
 
How To Create Portable Modules Using PowerShell In MacOS
 
To test if we have installed it properly or not use the below command:
 
pwsh
 
How To Create Portable Modules Using PowerShell In MacOS
 

Create PowerShell Custom Module

 
To Create Custom module we will use .net core for creating it. Assuming that we have installed .net core already in the environment now let us install it. 

To install .net core module for creating custom PowerShell module use the below command:
 
dotnet new --install Microsoft.PowerShell.Standard.Module.Template::0.1.3
 
How To Create Portable Modules Using PowerShell In MacOS
 
Now let use create a project for creating custom PowerShell module. To scaffold the project use the below command:
 
dotnet new psmodule
 
How To Create Portable Modules Using PowerShell In MacOS
 
There would be bin, obj folder and .csproj and .cs file in the project. Open the code in Visual Studio to check the code.
 
As displayed in the image below to create a custom command we have to inherit the class PSCmdlet and the below three methods define the execution.
  • BeginProcessing() - This method is called when the pipeline starts execution.
  • ProcessRecord() - This method is called for every input which is passed to the command.
  • EndProcessing() - This method is called at the end of pipeline execution.
How To Create Portable Modules Using PowerShell In MacOS
 
To build the project use the below command:
 
dotnet build
 
Open PowerShell using the below command:
 
pwsh
 
Once we have build the module now we can import it to test our module
 
Use the below command to import the module:
 
Import-Module .\bin\debug\netstandard2.0\powershellmodule.dll
 
How To Create Portable Modules Using PowerShell In MacOS
 
Test-SampleCmdlet -FavoriteNumber 3 -FavoritePet Dog
 
How To Create Portable Modules Using PowerShell In MacOS
 

Conclusion

 
It is very easy to create custom PowerShell module using any platform of our choice. This module is reusable so once we have developed our custom module we can use it anywhere we want.