0
votes

Could someone provide PowerShell script to split the Module (.psm1) to Functions (.ps1).

Using "Get-Content" I able to read the content of .psm1 file but I couldn't able to export it to functions.

1

1 Answers

1
votes

Below PowerShell script will split the Module (.psm1) to Functions (.ps1).

$ModuleName = 'Module1' #Specify the module name
$SplittedFunctionPath = "D:\SplittedFunction\" #Specify Splitted function path

#Import-Module
Import-Module $ModuleName

#Function to split the module and export it as functions
Function Insert-Content 
{
     param ( [String]$Path )
     process 
     {
     $( ,$_; Get-Content $Path -ea SilentlyContinue) | Out-File $Path
     }
}
$FunctionName = Get-Command -Module $ModuleName 
$path ="$SplittedFunctionPath" 
Foreach ($Function in $FunctionName.Name)
{
   (get-command $Function).definition | out-file $Path\$Function.ps1
   "Function $Function `n {" | Insert-Content $Path\$Function.ps1
   "}" | Add-Content $Path\$Function.ps1

}