Presents a comprehensive coverage of how to create and use PowerShell script modules plus some advanced concepts like creating a module manifest and how to have a module dynamically loads functions from individual script files.
Hi Bryan - Stumbled across this video and wanted to express appreciation. I'm a OOP coder who's well-versed with PowerShell, but never took the time to learn modules, manifests, and the like the right way. This was an awesome way to "cheat" myself back into compliance! Also, importing as a custom object is an awesome crutch for me to feel better about the inherent sloppiness of scripting! Thanks!!
Hi Jeremy, Thanks. Yeah. It is a quick and easy way to do OOP. PowerShell does now support True classes and inheritance but I like the ease of this method of getting the same effect.
perfect, as I was trying to do same approach @ 3:50 spot on.... 5:58 it is indeed a library or atleast the way I try to do it that way too breaking it down. I am really happy I stumbled upon this video as I just started doing babysteps towards powershell although I do have small background on programming, going through the video feels like I'm just about to get in to the good part thx thx
In my PS modules, i always throw in the .psd1 and also have that dot sourcing! Or import-module instead of . (Never checked if there are any difference part from been run.. gotta do that i guess 😓)
I really love the simplicity of maintaining code with the dot sourcing, but my experience is that when I do a Remove-Module does not take the loaded functions into consideration. I have no idea how that would effect performance with time?
Script modules are a better approach than dot sourcing in most cases as it provides easier code maintenance and code sharability. However, you can remove functions not loaded via module too. See powershell-guru.com/powershell-tip-35-list-and-remove-a-function/
In my powershell file First.ps1 i have a Function Sum. In the Second.psm1 module i have the Functions: Sum, Multiply. How to only Import Multiply into First.ps1.
You can use the -FUNCTION parameter of the Import-Module cmdlet. See docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/Import-Module?view=powershell-7
I agree. Definitely use source code control. The problem is when a function has been duplicated multiple times, in separate scripts, there is more work to maintain the code. In PowerShell, there is a tendency to write many one-off utility scripts, perhaps with slight differences or even hard-coded values, rather than extracting them to functions and putting them in a module. Thanks
This isn't working for me. I created the module (.psm1) file exactly like yours, and I have one .ps1 file in my function directory, but when I import the module it doesn't import the function. Am I correct in understanding that a .psm1 file is basically just a script that runs when you import the module?
I got it working by changing the path concatenation in my loop to this: #Loop through the list of files and load them foreach ($function in $functionList) { $fullPath = $functionDirectory + $function Write-Host 'Importing file: ' $fullPath . $fullPath } For some reason it wasn't liking {$functionDirectory + $function}
Nabeel Zaidi if you run the saved script with the $PSScriptRoot you will get the folder path, you cannot run it in other way. 1. Saved as file. 2. Script is run or in debugging mode, mostly no other way.. i’m just 7months late to answer.. sorry 😅✌️