Assuming the following Powershell script...
function Process-Folder {
[cmdletbinding()]
param ([string]$Path)
Process {
$returnvalue = ""
# code here that reads all of the .txt files in $path
# and concatenates their contents to $returnvalue
return $returnvalue
}
}
I'd like to add lines to this script which call this function a couple of times to process several folders. I would write that code as follows:
$allFileContent = ""
$firstFolder = Process-Folder -Path "c:\foo"
$allFileContent = $allFileContent + $firstFolder
$secondFolder = Process-Folder -Path "c:\bar"
$allFileContent = $allFileContent + $secondFolder
This code works but seems inelegant and doesn't seem like "the Powershell way". I tried:
$filecontent = ""
$filecontent = $filecontent + Process-Folder -Path "C:\foo"
$filecontent = $filecontent + Process-Folder -Path "C:\bar"
But ISE gave me "unexpected token 'Process-Folder' in expression or statement. I also tried:
$filecontent = ""
$filecontent | Process-Folder -Path "C:\foo"
$filecontent | Process-Folder -Path "C:\bar"
Which returned...
The input object cannot be bound to any parameters for the command either because the
command does not take pipeline input or the input and its properties do not match any
of the parameters that take pipeline input.
How can I accomplish what the first snippet does in a more elegant/"Powershell-like" way?