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
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s