Powershell Basics: Working with Modules

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone so in this video we're going to look at modules in powershell we're going to start with default modules that are available to you when you install powershell then we're going to see the components of modules so we have psm1 file and psd1 file we're going to talk about what are those then we're going to see custom modules that you can leverage in your project those are written by other developers and that's one of the powerful feature of powershell we have lots and lots of modules already available to you in something called powershell gallery that you can leverage in your project and in the end we are going to also work on a writing custom module and see how we can use it in powershell okay so let's start going through the default modules let's open terminal and go to powershell okay so let's see what modules are already available to us we can check that so we have one module which is imported installed and imported however this is not the only module that is available to view all the available modules you need to have tag list available parameter and now you see all these are the modules that are available to you to view commands inside module you can type get command and give the module name so we can specify microsoft let's look microsoft powershell management module so these are all the commandlets that are available inside powershell management microsoft partial management module okay now let's clear the screen so next thing what we want to see is when you type get module right let's when we try to get module and we gave the list available now where are all these modules available so there are three places that powershell looks to retrieve all the modules so by default powershell is going to look into these three places for getting the system modules all user modules and current user modules so system modules is by default where all the package that you import and install are going to be placed all user modules is anybody who logs into the machine those modules will be available for them and current user modules are specifically for your usage something that you want when you log in so these are user specific user specific modules and that's why they are placed under the specific user directory so these are the places and then you can also specify your own custom path also using the environment variable ps module path now let's look at importing modules so there are two ways to import modules one by default actually with the powershell's auto importing feature powershell is smart enough to identify which modules has that command when you run a command it identifies which modules that the command belongs to and auto imports that module so let's see an example for that so i'll open up a new terminal window okay let's start a new session and here let me type again get module so we see there is only one module currently available now what happens when i type cd home directory and then if i type get module you see there is a new module that is also imported home directory cd slash is actually nothing but a set location command led that is run behind the scene which is available in powershell management module so this is the auto import feature of powershell okay so now what if we want to explicitly import a module so for that we type import module and we can specify the name parameter tag name and we can again type this module name so here suppose we want to have easy module so it's going to take time and it's going to load our easy module now if i check get module so i see all the easy modules are also available installed and available so that's how you explicitly import modules okay so now let's look at the components of powershell module so there are two components of a module one is the com module itself and it's stored as dot psm1 file and uh we have something called module manifest file which is which is stored with the extension of ps dot psd one file so let's look at how these files are created so for that i'm going to switch to visual studio which would be easier so let me and i'll open visual studio okay so here let me create a new file and we are going to call it test function or psm 1 so this is our module file so it has to have the extension of dot ps1 dot psm 1. now this file should contain function at least one function when you create a psm1 file the best practice is to have all the functions inside that that has the same noun so as you know powershell command let and functions the best practice is used is to use the verb noun pair so we have set location we have get module get command so the same the same practice has to be followed here and then you should have all the functions with the same noun so you can have for example if you you're creating a module that does operation on software so you can have get software you can have remove software you can install software so all your function should have the software as the noun so that's the best practice so let's see let's take the same example and see suppose i want to create this test function and have three three function inside that right so let's say get software now let's do another function we can say install software and then we can say remove software okay so let's save that so this is our psm1 file and this has three functions in it so very good now this is the first first component for creating module the second component as i was talking is to have a psd one file so to create that there's an easy way what you can do is you can open terminal and you can go to powershell and then this is the command to have a default or initializer manifest file so you type new module manifest you can give path so let me just copy path of this location dot psd one let me replace with that and then you can specify a few options so you can have let me tag a new line you can specify the author let's say my user and then you can specify another argument root module and that you can tie with test test function or psm1 and then you can give uh let me go to new line you can give a description module used for deploying software okay now if you enter or we see here test function.pst1 is created and this is the module manifest file this is a kind of a hash table so it has key value pairs and it gives the information about the module and it initializes some default parameters so that's about the components of mod so now let's see how do we actually use the module that we have created the test function module how we can actually get that module displayed under get modules command command led so to do that we need to place our test function.psa m1 module file into this directory so let's copy this okay so one of the main rule and this this you must remember when you try to create your own modules and you place it in a directory in any of the one of the three directories you must have the folder name same as your psm1 file this is the most important rule if you don't give the same name it's not going to list in your powershell so let me create a new folder and i have to call it same name so test function and inside that i need to place my let me do go to this folder copy these okay so now that this is done now let's run it again and we should see a test function somewhere here let's see get module list yes so now we see the function now we see the module and we see the functions also that are available in this so that's how you can actually create your own module and you can then leverage that functions that you've written you can import and then use it okay that's all for this video thanks for joining
Info
Channel: Brewed Iced Code
Views: 248
Rating: 5 out of 5
Keywords: Powershell modules, modules in powershell, powershell basics, powershell, import modules in powershell, custom modules, creating your own modules
Id: egbg8MSAbUo
Channel Id: undefined
Length: 12min 52sec (772 seconds)
Published: Sun Jan 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.