You probably use “Send-MailMessage” across many scripts. I sure do.
Why repeat configuration in every script? That’s a headache to update and manage long term. I suggest keeping that configuration in Machine Environment Variables. These save globally and are visible to any downstream code including:
- Dot Net
- Windows Service
- Windows Form
- .. and more
Below is an example using “$env:smtp” to send email from PowerShell. Now all of your scripts are simpler and as you move PS1 files across environments (dev, test, acpt, prod) the configuration will follow naturally. Cheers!
Send-MailMessage -To "email@example.com" -From "firstname.lastname@example.org" -Subject "hello world" -Body "hello world" -SmtpServer $env:smtp