Quantcast
Channel: admin – PEETERSONLINE.NL
Viewing all articles
Browse latest Browse all 20

Convert Scripts to Module

$
0
0

I used to have all my common functions stored as separate scripts in a single folder. I’d load these through a few lines of code in my profile, looping through the script files and dot-sourcing them. Powershell v2.0 has a new feature called Modules. So how do you convert a bunch of scripts (containing functions) into a single module you can load or unload whenever you feel you might need those functions?

Here’s how:

# Convert function scripts to Module
 
#Variables
$myModuleName = "myModule123"
$inputFolder = 'D:\scripts\Functions'
$outputFolder = "$PSHOME\Modules\$myModuleName"
 
#Create the module directory
$null = New-Item $outputFolder -ItemType directory
 
#Create the module with a nice oneliner
Get-ChildItem $inputFolder -Filter "*.ps1" | Get-Content | Out-File "$outputFolder\$myModuleName.psm1" -Append
 
#Import the module
Import-Module $myModuleName
 
#Show functions loaded from the module
Get-Command -Module $myModuleName

Viewing all articles
Browse latest Browse all 20

Trending Articles