Search code examples
powershellscriptingpowershell-cmdletget-childitem

Adding output data to 1 text file


As you can see below, the content from all .msg files within a folder is extracted. However, I can only see 1 .msg content in the output content.txt and not all the .msg files. How can I loop the command and add to the same .txt file?

$output_file3 = ‘c:\outlook_files\content.txt’

# Search messages
Get-ChildItem "c:\outlook_files\msg\" -Filter *.msg | Foreach-Object {
    # Read current message content
    $content = Get-Content $_.FullName >$output_file3
}

Solution

  • Instead of using redirection, use Out-File -append or Add-Content

    so change

    $content = Get-Content $_.FullName >$output_file3

    to

    Get-Content $_.FullName | out-file $output_file3 -append

    I dropped the variable assignment because it is not doing anything in this context.