Saving all functions in a module to separate files

I like to keep functions in separate files. I figured I’d have a go at extracting all the functions from my recently downloaded PowerShellCookbook module into separate files. I also wanted to make sure that the filenames generated would match the function names.

Here’s what I came up with. It’s basically a one liner ‘steroided‘ (take a look if you don’t know what I mean) to make it more readable. The only caveat really is that the module needs to be loaded into memory, as it uses the Function PS Provider to obtain the information.

If you wanted to, you could completely omit the Where-Object filter to dump all currently loaded functions into separate files.

#requires -Version 3
Get-ChildItem -Path Function:\  |
Where-Object -Property ModuleName -EQ -Value ‘PowerShellCookbook’ |
Select-Object -Property Name, ScriptBlock |
ForEach-Object -Process {
Add-Content -Path “d:\temp\$($psitem.Name).ps1″ -Value $psitem.ScriptBlock

Leave a Reply

Your email address will not be published. Required fields are marked *