Thursday, April 25, 2024
HomePowershellEasy methods to create a PowerShell Module with a number of Features...

Easy methods to create a PowerShell Module with a number of Features – SID-500.COM


On this weblog put up, I’ll present you easy methods to create a module with a number of capabilities utilizing an instance. You will notice that this isn’t rocket science. Let’s leap in.

With a view to use a number of capabilities in a module, we’ve got to declare them as capabilities to export. This additionally means we want a module manifest file with that statements in it.

Goal

The aim of this tutorial is to supply two capabilities in a module named Patrick. The 2 capabilities are known as Get-SmallFiles and Get-BigFiles.

Making a Module Folder “Patrick”

To start with, we have to create a module folder. We are able to select between three folder paths. I’ll decide the primary possibility, the folder is then created in C:Program FilesWindows PowerShell, which implies that this module will likely be out there to all customers.

Right here is the code for my folder “Patrick”.

New-Merchandise -Path ($env:PSModulePath -split ';')[0] -ItemType Listing -Title Patrick

Now we’ve got a folder within the module folder!

Creating the Module Manifest File “patrick.psd1”

As talked about earlier than, we want a psd1 file which serves as our manifest file. We offer all the knowledge wanted, particularly the 2 capabilities: Get-SmallFiles and Get-BigFiles.

$paramHash = @{
    Path="C:Program FilesWindowsPowerShellModulesPatrickpatrick.psd1"
    RootModule = "patrick.psm1"
    Creator = "Patrick Gruenauer"
    CompanyName = "sid-500.com"
    ModuleVersion = "1.0"
    Guid = '56f62aec-33ca-4d8a-8685-c6a87ac9a002'
    PowerShellVersion = "5.1"
    Description = "Testmodul"
    FormatsToProcess = ""
    FunctionsToExport = @('Get-BigFiles', 'Get-SmallFiles')
    AliasesToExport = ""
    VariablesToExport = ""
    CmdletsToExport = ""
   }
   
   New-ModuleManifest @paramHash

Let’s transfer on with the creation of the PowerShell Module file.

Creating the PowerShell Module File (psm1) with two capabilities: Get-SmallFiles and Get-BigFiles

Subsequent, we configure the PowerShell module file with all of the capabilities in it, in my case it’s Get-SmallFiles and Get-BigFiles.

New-Merchandise -Path 'C:Program FilesWindowsPowerShellModulesPatrick' -ItemType File -Title patrick.psm1

The file is empty. Let’s put some content material in it.

Add-Content material -Path 'C:Program FilesWindowsPowerShellModulesPatrickpatrick.psm1' `
-Worth 'operate Get-BigFiles  The place-Object Size -gt (1MB); 
operate Get-SmallFiles  The place-Object Size -lt (1MB)'

Mission completed.

How have you learnt this labored?

Open PowerShell 5 or PowerShell 7.

Sort Get-Smallfiles and afterwards Get-BigFiles.

Hope this was helpful. See you subsequent time.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments