Hello,
The behavior you’re describing is a classic difference between interactive execution and scheduled execution in Task Scheduler. When you run the script manually in PowerShell 7, the environment has your user profile loaded, including credentials, module paths, and any implicit authentication context. When Task Scheduler runs it, the job runs in a different security context and often without the same environment variables, which is why the report file is generated but the SMTP send fails.
The first thing to check is the account under which the scheduled task is running. If it is set to “Run whether user is logged on or not,” the task runs in a non‑interactive session that may not have access to stored credentials or the PowerShell 7 path. Ensure that the “Program/script” field points explicitly to the PowerShell 7 executable, for example:
C:\Program Files\PowerShell\7\pwsh.exe
and that the “Add arguments” field includes the full path to your script and parameters. If you are currently calling the script directly, Task Scheduler may be defaulting to Windows PowerShell 5.1, which does not handle the PowerCLI module the same way.
Next, confirm that the SMTP send portion of the script does not rely on interactive authentication. If you are using Send-MailMessage or a similar cmdlet, credentials must be explicitly provided or the SMTP server must allow relay from the scheduled task’s account. In interactive mode, cached credentials may allow it to work, but in scheduled mode those are not available. You can test this by running the script with -Credential parameters or by configuring the SMTP server to accept the scheduled task account.
Finally, check the Task Scheduler history and the script’s error output. Redirect the error stream to a log file so you can see whether the failure is due to authentication, module loading, or path resolution. A common issue is that the PowerCLI module is not loaded in the non‑interactive session. To fix this, add an explicit Import-Module VMware.PowerCLI at the start of the script and ensure $env:PSModulePath includes the PowerCLI install directory.
In short, the file output works because it doesn’t depend on external authentication, but the email send fails because Task Scheduler is running in a different context without the same credentials or environment. Align the scheduled task to run under PowerShell 7 with explicit module imports and SMTP credentials, and the email portion should succeed.
I hope you've found something useful here. If it helps you get more insight into the issue, it's appreciated to accept the answer. Should you have more questions, feel free to leave a message. Have a nice day.
Domic Vo.