The script I'm using is as follows:
# Enter the file path for your csv here
# note: there needs to be a header named "userEmail" in the first row
$csvArray = Import-Csv "file\path\here" -Filter "file.csv"
# Fill out with necessary information, send to all emails in the csv
foreach ($item in $csvArray) {
$outlook = new-object -comobject outlook.application
$template = Get-ChildItem("C:\Users\Name\AppData\Roaming\Microsoft\Templates") -Filter "myTemplate.oft"
$email = $outlook.CreateItemFromTemplate($template)
$email.To = $item.userEmail
$email.send()
}
The error I am getting is:
Exception calling "CreateItemFromTemplate" with "1" argument(s): "We can't open 'myTemplate.oft'. It's possible the file is already open, or you don't have permission to open it. To check your permissions, right-click the file folder, then click Properties."
- the file is not open anywhere else
- I've opened and closed Outlook multiple times
- I've also opened Outlook in safe mode and get the same error
- I have permissions for these templates
- I removed myself and added myself to the permissions just to double check that it wasn't stuck somewhere there.
when I first wrote the script it worked a couple times during testing, now I always get this error.
There is also nothing special about the template, it's just a normal email.
I'm also a beginner in powershell, so it's possible I'm just doing something wrong.