# windows powershell mail error

**URL:** <https://forums.powershell.org/t/windows-powershell-mail-error/7155>\
**Category:** PowerShell Help\
**Created:** [September 7, 2016, 11:52pm UTC](https://forums.powershell.org/t/windows-powershell-mail-error/7155 "2016-09-07T23:52:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkatesh-sql](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@venkatesh-sql](https://forums.powershell.org/u/venkatesh-sql)\
**Post date:** [September 7, 2016, 11:52pm UTC](https://forums.powershell.org/t/windows-powershell-mail-error/7155/1 "2016-09-07T23:52:32Z")

</div>

Hi,  
We have a requirement where files in a particular directory get processed the corresponding business users need to be intimated via email. All the Directories and Sub-directories have email associated and all this information is saved in a comma separated file.

$From = “[myemail@domain.com](mailto:myemail@domain.com)”  
$To = “[businessuser@domain.com](mailto:businessuser@domain.com)”  
$Cc = “[stakeholder@domain.com](mailto:stakeholder@domain.com)”  
$Subject = “File process intimation”  
$Body = “File processed”  
$SMTPServer = “[smtp.domain.com](http://smtp.domain.com)”  
$SMTPPort = “1739”

$flatFilePath = “C:\Temp\Email.csv”  
$flatFile = Import-Csv -Path $flatFilePath -Header Dir,Email  
$emailID = @{}  
foreach ($record in $flatFile) { $emailID[$record.Dir]=$record.Email }

# organization folder

$orgFolder = Get-ChildItem -Path "C:\Temp" -recurse -exclude _._

# Send email

foreach ($dir in $orgFolder) {  
$processedfiles = $null  
$emailTo = $null

```
Get-ChildItem -Path $dir.FullName -Recurse -include *.txt,*.xls,*.csv,*.xlsx | ? { $_.FullName } |  
    Foreach-Object { 
       [string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name
       $processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | ";  
    }

$emailTo = $emailID.Get_Item($dir.Name) -split ";"   

if ($processedfiles) { Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body "$Body $processedfiles" -SmtpServer $SMTPServer -port $SMTPPort } 

```

}

This is the ps script to send email. This works okay but when I have a file to process in “C:\Temp\Accounts\GL” and no included file in “C:\Temp\Accounts” it still sends out 2 different email to [accounts@domain.com](mailto:accounts@domain.com) & [accountsgl@domain.com](mailto:accountsgl@domain.com).

Data in comma separated file:

Dir Email  
Accounts [accounts@domain.com](mailto:accounts@domain.com)  
GL [accountsgl@domain.com](mailto:accountsgl@domain.com)  
AR [accountsar@domain.com](mailto:accountsar@domain.com)  
AP [accountsap@domain.com](mailto:accountsap@domain.com)  
Sales [sales@domain.com](mailto:sales@domain.com)  
SalesOrder [salesorder@domain.com](mailto:salesorder@domain.com)

Thank you.

---

<div class="post-metadata">

**Author:** ![84rusty](https://avatars.discourse-cdn.com/v4/letter/8/ac8455/32.png) [@84rusty](https://forums.powershell.org/u/84rusty)\
**Post date:** [September 8, 2016, 3:26am UTC](https://forums.powershell.org/t/windows-powershell-mail-error/7155/2 "2016-09-08T03:26:07Z")

</div>

Does this work for you?

```
$From = "myemail@domain.com"
	$To = "businessuser@domain.com"
	$Cc = "stakeholder@domain.com"
	$Subject = "File process intimation"
	$Body = "File processed"
	$SMTPServer = "smtp.domain.com"
	$SMTPPort = "1739"

	$flatFilePath = "C:\temp\Email.csv"
	$flatFile = Import-Csv -Path $flatFilePath -Header Dir,Email

	$emailID = @{}
	foreach ($record in $flatFile) { 
		$emailID[$record.Dir]=$record.Email
	}

	# organization folder
	$orgFolder = Get-ChildItem -Path c:\temp -recurse -exclude *.*

	# Send email
	foreach ($dir in $orgFolder) {

		$processedfiles = $null
		$emailTo = $null

		Get-ChildItem -Path "$($dir.FullName)\*.*" -include *.txt,*.xls,*.csv,*.xlsx | ? { $_.FullName"} | Foreach-Object {

			[string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name
			
			$processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | "

		}

		$emailTo = $emailID.Get_Item($dir.Name) -split ";"

		if ($processedfiles) { 
			Send-MailMessage -From $From -to $emailTo -Cc $Cc -Subject $Subject -Body "$Body $processedfiles" -SmtpServer $SMTPServer -port $SMTPPort 
		}
	}
```

---

<div class="post-metadata">

**Author:** ![venkatesh-sql](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@venkatesh-sql](https://forums.powershell.org/u/venkatesh-sql)\
**Post date:** [September 8, 2016, 8:54am UTC](https://forums.powershell.org/t/windows-powershell-mail-error/7155/3 "2016-09-08T08:54:46Z")

</div>

Hi, For some reason file process duplication happens. My folder structure is “C:\Temp\SalesOrder\20160809\cc.txt” when its looking for all the files that need to be processed as soon as the recursion gets to folder “C:\Temp\SalesOrder” & “C:\Temp\SalesOrder\20160809” its processing cc.txt file. How do I rewrite the gci to not to traverse subfolders and only process files “C:\Temp\SalesOrder\20160809”

Problem area:  
Get-ChildItem -Path $dir.FullName -Recurse -Include _.txt,_.xls,_.csv,_.xlsx | ? { $_.FullName } | Foreach-Object { [string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name $processedfiles += [string][System.IO.Path]::GetFileName($\_.FullName) + " | "; }

I want to process files only when the file that meets the criteria exists. For some reason even though I have just 1 file to process in all the folders put together it does process more than once and cannot figure out why? I have no hidden files either.

Thank you.

---

<div class="post-metadata">

**Author:** ![84rusty](https://avatars.discourse-cdn.com/v4/letter/8/ac8455/32.png) [@84rusty](https://forums.powershell.org/u/84rusty)\
**Post date:** [September 8, 2016, 2:59pm UTC](https://forums.powershell.org/t/windows-powershell-mail-error/7155/4 "2016-09-08T14:59:22Z")

</div>

Hi venkatesh,

Check out the code I supplied in my previous post, it is your code just with the fix applied.

Specifically you mention the gci section as a problem, so remove the -Recurse, and add _._ onto the -path so it picks up the files, like so:  
Get-ChildItem -Path "$($dir.FullName)\*._" -include .txt,.xls,_.csv,\*.xlsx

You don’t need to recurse on this call as you recursed earlier ($orgFolder = Get-ChildItem -Path c:\temp -recurse -exclude _._)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/windows-powershell-mail-error/7155/5 "2024-05-16T20:40:21Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


