#requires -version 2.0 #Jeffery Hicks #Twitter: @JeffHicks #http://jdhitsolutions.com/blog #To learn more about PowerShell check out : # Windows PowerShell 2.0: TFM # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** Function Export-Function { <# .Synopsis Export a PowerShell function to a file. .Description Get a Windows PowerShell function and export it to a file. You can export functions individually, each to its own file, or you can export them to the same file using -Append. By default the filename will be a PowerShell script file with the same name as the function, but you can specify an alternate name. See examples. .Parameter Name The name of the function to export. .Parameter Path The folder for the exported function. .Parameter Filename The filename for the exported function. The default is the function name. .Parameter Append Write each function to the same file. .Example PS C:\> Export-Function "MyFunction" This will export the MyFunction function to a script file called MyFunction.ps1. .Example PS C:\> dir function:*ISE* | Export-Function -path c:\export -filename ISEFunctions.psm1 -append Get all functions with ISE in the name and export them to a module file C:\export\ISEFunctions.psm1 .Inputs Strings for function names .Outputs None .Link About_Functions Function (provider) Get-Command .Notes VERSION: 1.1 This function supports cmdlet binding so you can use -Verbose and -Whatif. #> [cmdletbinding(SupportsShouldProcess=$True)] Param( [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$True, HelpMessage="Enter the name of a current function to export")] [ValidateNotNullorEmpty()] [string]$name, [Parameter(Position=1,HelpMessage="Enter the folder to store the file.")] [ValidateScript({Test-path $_})] [string]$path="C:\scripts", [Parameter(Position=2, HelpMessage="Enter the name of the file to create. The default is the function name.")] [ValidateNotNullorEmpty()] [string]$filename="$name.ps1", [switch]$append ) begin { Write-Verbose "Starting $($myinvocation.mycommand)" #initialize a counter $i=0 } process { #verify function exists try { $myfunction=Get-Command -Name $name -ErrorAction "Stop" } catch { Write-Warning "Failed to find function $name" Return } #function found so continue if ($myFunction) { #strip out illegal filename characters from function name [regex]$r="[:/\\]" if ($r.matches($filename)[0]) { Write-Verbose "Removing illegal filename characters from $filename" $r.matches($filename) | foreach { $filename=$filename.replace($_.value,"") } } #if $r.matches $filepath=Join-Path -Path $path -ChildPath $filename #this command needs to be left justifie so the final result is nicely #formatted $output=@( "{0} {1} {2} {3} {4} #end function ############################################################################### " -f $myFunction.CommandType,$myFunction.name,"{",$myFunction.definition,"}") if ($pscmdlet.ShouldProcess($name)) { #increment the counter by 1 $i++ if ($append) { write-Verbose "Exporting and appending to $filepath" $output | Out-File -FilePath $filepath -Append } else { Write-Verbose "Exporting $name to $filepath" $output | Out-File -FilePath $filepath } } #if shouldprocess } #if $myFunction } end { Write-Verbose "Ending $($myinvocation.mycommand)" Write-Host "Exported $i functions" -ForegroundColor Green } } #end function