Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Help with powershell script to read a folder of text files + extract 1 line from each


Recommended Posts

Posted

We use the Mdaemon email platform.

 

I have a folder of emails, all replies to a question I sent out.

 

I would like to create a text file of all the users who have replied.

 

This is the format of a typical file:

 

X-MDAV-Result: clean
X-MDAV-Processed: school.domain.com, Tue, 27 Mar 2018 10:37:36 +0100
Return-path: 
Authentication-Results: school.domain.com;
auth=pass (login) [email protected]
Received: from WorldClient by school.domain.com (MDaemon PRO v17.5.2) with ESMTPA id md50007290673.msg; 
Tue, 27 Mar 2018 10:37:36 +0100
X-Spam-Processed: school.domain.com, Tue, 27 Mar 2018 10:37:36 +0100
(not processed: message from trusted or authenticated source)
X-MDArrival-Date: Tue, 27 Mar 2018 10:37:36 +0100
X-Authenticated-Sender: [email protected]
X-Rcpt-To: [email protected]
X-MDRcpt-To: [email protected]
X-Return-Path: [email protected]
X-Envelope-From: [email protected]
X-MDaemon-Deliver-To: [email protected]
X-CAV-Result: clean
Received: from [iP ADDRESS] by school.domain.com via WorldClient with HTTP;
Tue, 27 Mar 2018 10:37:32 +0100
Date: Tue, 27 Mar 2018 10:37:32 +0100
From: "FistName Surname" 
To: "IT Manager" 
Subject: Re: [staff] Quick Question ! {01}
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Message-ID: 
X-Mailer: WorldClient 17.5.2
In-Reply-To: 
References: 

Yes please. Thanks 

 

I'd like to extract the line:

 

From: "FistName Surname"

 

from all the other email/files and dump in to one text file.

 

 

Anyone have any scripting experience to help with this?

Posted

Should do the trick, just change the $FolderPath and $OutputFile variables to the appropriate

 

$FolderPath = "C:\Emails"
$OutputFile = "C:\EmailsFrom.txt"

$Emails = Get-ChildItem -Force $FolderPath -Recurse

ForEach ($Email in $Emails) {
   $regex = [regex] "^From: (.*)$"
   Get-Content $Email.FullName | ForEach-Object {
       If ($_ -match $regex) {
           $_ | Out-File -FilePath $OutputFile -Append
       }
   }
}

Posted

$filesFolder = "files"

$outputFile = "output.txt"

 

 

$files = Get-ChildItem $filesFolder

for ($i=0; $i -lt $files.Count; $i++) {

$currentFile = $filesFolder + "" + $files[$i]

 

foreach ($text in get-content $currentFile){

if ($text.Length -gt 6){

if ($text.Substring(0,6) -eq "From: "){

Add-Content $outputFile $text

}

}

}

}

  • Thanks 1
Posted
Should do the trick, just change the $FolderPath and $OutputFile variables to the appropriate

 

$FolderPath = "C:\Emails"
$OutputFile = "C:\EmailsFrom.txt"

$Emails = Get-ChildItem -Force $FolderPath -Recurse

ForEach ($Email in $Emails) {
   $regex = [regex] "^From: (.*)$"
   Get-Content $Email.FullName | ForEach-Object {
       If ($_ -match $regex) {
           $_ | Out-File -FilePath $OutputFile -Append
       }
   }
}

 

Works perfectly - many thanks...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...