Creating a Custom PowerShell Module: Files and Folders
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Now it's time to create a PowerShell module, and we're going to get started by creating the files and folders that we're going to need for this module. And this is actually really exciting because this is a pretty advanced topic, but I'm going to break it down in such a way that anybody can get started doing this again.
You want to make a module whenever you find yourself reusing code over and over again, and maybe different function files or maybe different scripts that you have. Maybe you have a function that you use multiple times throughout multiple scripts. Well, maybe it's time to look at creating your own personal module.
So to get started, what we're going to do is just open PowerShell. And again, I'm running it as an administrator. What we're going to do is find the actual folder path that Where we're going to be installing or creating this module. So again, the environment colon PS module path. This is something that we've done before.
I like to split this dot split, and I like to split it by the semi-colon just because it puts each module path on a single. Now I have this. He uses administrator documents. PowerShell. If I copy this path. I can browse to it. If you get an error message saying you can't browse to this directory, copy back one directory, and just don't do the modules, do the one back, and then you probably have to manually create the modules folder.
It probably just doesn't exist yet. So what we're gonna do… is create our own folder here, and we're going to make a module that is going to compose a kind of like a hello world message or a greeting message that it just gives us a good example of how to create PowerShell modules. We're going to call this module GreetMe, so I'm going to right-click and say new folder, and I'll call it GreetMe.
Now the name of the actual folder does matter. It needs to match the PSM 1 file that we will create inside the folder. Remember, if you name this something else, you need to update all the other names of the files to make sure that it matches. So I would recommend that you just give it the same name.
You'll save yourself a lot of time. All right. Now that we've done that, what I'm going to do is right-click and use open with code. And what this is going to do, it's going to open the folder in visual studio code. And the first thing we're going to see is do you trust the authors of the files in this folder?
I will say yes; I trust the authors of all files and the parent folder modules. What this means is that I won't have to get this pop up for anymore. Modules that I create or any more modules that I copy into this directory. So if you're uncomfortable doing that, you don't have to check it.
It's totally up to you. I'm going to check it, and I'm going to say, yes, I trust the authors because I am the author. So now what I'm going to do is maximize this, and we're going to get started by creating some of the folders here. The first thing we're going to do is make a new folder, and we're going to call this public.
And this is where all of our public functions are going to go we want to be accessible from outside the module, which means when you, when a user imports our module. What functions are they going to have access to? That's going to be all the functions that we placed inside this public folder.
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.