# A script that sends emails via the OUTLOOK SMTP server

**URL:** <https://forums.powershell.org/t/a-script-that-sends-emails-via-the-outlook-smtp-server/13921>\
**Category:** PowerShell Help\
**Created:** [March 5, 2020, 11:20am UTC](https://forums.powershell.org/t/a-script-that-sends-emails-via-the-outlook-smtp-server/13921 "2020-03-05T11:20:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadav-sh](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@nadav-sh](https://forums.powershell.org/u/nadav-sh)\
**Post date:** [March 5, 2020, 11:20am UTC](https://forums.powershell.org/t/a-script-that-sends-emails-via-the-outlook-smtp-server/13921/1 "2020-03-05T11:20:41Z")

</div>

I got a task to do a script in PowerShell where I need to get out of AD a list of names that begin with the letter “A” with full name, username and creation date criteria. Then export the list with custom headers and export it to a CSV file Then import the CSV file again and output anyone who works over two years (full name) So far I have done the script correctly in my opinion because it works. Now I’m pretty stuck I need to make every user on the list who went out first to make a folder (no matter where) and give full permissions to the folder - **I did something but it seems to be incorrect.** Then I need to send an email with a table that states the full name and folder path and that the sender will be [no@reply.com](mailto:no@reply.com).

The code I wrote:

```
 Get-ADUser -filter * -SearchBase "OU=meitavdash,OU=Users,DC=meitav,DC=co,DC=il"

    Get-ADUser -Properties whenCreated -Filter {samaccountname -like 'A*'} |Format-Table name,SamAccountName,whenCreated

    Get-ADUser -Properties whenCreated -Filter {samaccountname -like 'A*'} | Select-Object @{n='FullName';e={$_.Name}},@{n='UserName';e={$_.SamaccountName}},@{n='CreateDate';e={$_.WhenCreated}} | export-csv -path c:\userexport.csv
    Get-ADUser -Properties whenCreated -Filter {samaccountname -like 'A*'} | where {$_.whencreated -le (Get-Date).AddYears(-2)}|Select-Object @{n='FullName';e={$_.Name}} 

    $sp=$((Get-Date).AddDays(-365*2)); Get-ADUser -Properties whenCreated -Filter {name -like "A*" -and whenCreated -ge $sp}|%{New-Item -Path "c:\temp" -ItemType Directory; icacls "c:\temp" /T /grant "$($_.samaccountname):(OI)(CI)F"
```

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [March 5, 2020, 12:57pm UTC](https://forums.powershell.org/t/a-script-that-sends-emails-via-the-outlook-smtp-server/13921/2 "2020-03-05T12:57:56Z")

</div>

No need to search Active Directory multiple times. Search AD once then put results into a variable.

```
# Check AD one time for users
$user = Get-ADUser -Properties whenCreated -Filter {samaccountname -like 'A*'} -SearchBase "OU=meitavdash,OU=Users,DC=meitav,DC=co,DC=il" | 
Select-Object name,samaccountname,whencreated

# Export list with custom headers
$user | ConvertTo-Csv | ConvertFrom-Csv -Header FullName,UserName,CreateDate |
Select-Object -Skip 1 | Export-Csv -NoTypeInformation -Path .\user.csv

# List two year employees
$user2 = Import-Csv .\user.csv | 
Where-Object {$_.CreateDate -ge (get-date).AddYears(-2)}

# Create temp folder for each user
$list = [System.Collections.ArrayList]@()
Import-Csv .\user.csv | ForEach-Object {
    New-Item -Path "C:\Temp\$($_.UserName)" -ItemType Directory
    icacls "C:\Temp\$($_.UserName)" /T /grant "$($_.UserName):(OI)(CI)F"
    $obj=[PSCustomObject]@{Name=$_.FullName ; Path="C:\Temp\$($_.UserName)"}
    $list.Add($obj) | Out-Null
}

# Set email properties
    $prop = @{
        From = 'no@reply.com'
        To = 'sender@mail.com'
        Subject = 'User Path List'
        Body = $list
        SmtpServer = 'your.smtp.server.com'
    }

Send-MailMessage @prop
```

---

<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:30pm UTC](https://forums.powershell.org/t/a-script-that-sends-emails-via-the-outlook-smtp-server/13921/3 "2024-05-16T20:30:20Z")

</div>

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


