PeetersOnline.nl
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 |
No related posts.
One Response to Convert Scripts to Module
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





[...] Dit blogartikel was vermeld op Twitter door afokkema. afokkema heeft gezegd: cool script by @hugopeeters – Convert Scripts to Module: http://bit.ly/eYystO [...]