Try the modules
The technique we use is to ensure that all our functions are loaded through the script module. We created a folder to store all the individual function files and further subdivided them into the appropriate categories. Once we have done this, we create a .psm1 file to tell the module what to load, and then add the module path to our PowerShell profile (if not at the default module location).
Folder structure
Module-Name\ Subfolder1\ Subfolder2\ ... Module-Name.psm1
.psm1 file
Module-Name.psm1 (located under the folder with the same name - required)
# Script Module for Company Functions Function Get-ScriptDirectory {
File profile
Microsoft.PowershellISE_profile.ps1 / Microsoft.Powershell_profile.ps1
$LocalLibraries = "C:\Local\Path\On\Disk\" $env:PSModulePath = $env:PSModulePath + ";$LocalLibraries"
The code above means that you do not need to store the module in the same place as the rest (useful in our case, since we use SVN for the version and transfer our materials to our team).
Summary
Repeat:
- Function file name as "func_ {Verb} - {Namespace} {Exist.}. Ps1"
- Create a top-level folder to store the module psm1 file
- Create subfolders to categorize and store features, such as Utilities, Active Directory, Exchange, etc.
- Create the file "psm1"
- Optional: add an additional module location to your profile
We attach the “ps1” files with “func_” so that when filling in the name of the function, if it is in the same directory as the file, it will not get confused. In addition, we add a namespace (company initials, etc.) in front, so that our function names do not interfere with other added functions.
Convenient reboot function
Another useful tip that may come in handy during development is to define the alias "reload" in our case, which will cause the module to reboot. This means that after you have changed the file, all you have to do is print it and it will be automatically added to the memory with your changes.
# Function to reload Module Function int_ModuleNameModuleLoad { Import-Module Module-Name -Force -WarningAction SilentlyContinue Write-Host "Module-Name Reloaded" } # Set Aliases If (-not(Get-Alias "reload" -ErrorAction SilentlyContinue)) { New-Alias -Name reload -Value int_ModuleNameModuleLoad -Force }
The reason I use "int_" instead of our usual naming structure is because this function is inside our profile, and I consider it to be an internal rather than a complete function.
Hope this gives you some good ideas, it worked out great for us!
-Adam