Introduction
PowerCLI is powerful when used interactively, but it becomes transformative when scheduled. From snapshot audits to VM reports, regular automation increases consistency and saves time. You can schedule any PowerCLI script using native tools like Windows Task Scheduler or cron in Linux/macOS environments.
In this article, you will learn to:
- Create reusable PowerCLI scripts
- Use Windows Task Scheduler to run scripts daily or weekly
- Use cron on Linux/macOS with PowerShell Core
- Secure credentials and log output
- Build a scheduling strategy for DR, audits, or cleanup jobs
Step 1: Prepare a PowerCLI Script
Save your automation script with a .ps1 extension. For example:
# C:\Scripts\VM-Report.ps1
Connect-VIServer -Server "vcenter.lab.local"
Get-VM | Select Name, PowerState, VMHost | Export-Csv "C:\Reports\VM_Report.csv" -NoTypeInformation
Disconnect-VIServer * -Confirm:$false
Test manually in PowerShell before scheduling.
Step 2: Schedule Script Using Windows Task Scheduler
A. Open Task Scheduler
- Press Windows key → search for Task Scheduler
- Click Create Task
B. General Tab
- Name:
PowerCLI - VM Report Generator - Run whether user is logged in or not
- Configure for: Windows 10 or Server
C. Triggers Tab
- New → Daily → Start at 8:00 AM → Repeat every 1 day
D. Actions Tab
- Action: Start a program
- Program/script: CopyEdit
powershell.exe - Add arguments: arduinoCopyEdit
-ExecutionPolicy Bypass -File "C:\Scripts\VM-Report.ps1"
E. Conditions & Settings
- Uncheck “Start only if on AC power”
- Enable “Run task as soon as possible after a scheduled start is missed”
Click OK, enter credentials, and you’re done.
Step 3: Schedule Script Using cron on Linux or macOS
PowerCLI works with PowerShell Core. Use crontab -e to add a job:
0 2 * * * pwsh -File /home/user/scripts/backup-report.ps1 >> /home/user/scripts/cron.log 2>&1
This runs every day at 2:00 AM and logs output to a file.
Validate with:
crontab -l
Diagram: PowerCLI Scheduling Workflow

Step 4: Use Credential Storage Securely
Use Windows Credential Manager with New-VICredentialStoreItem:
New-VICredentialStoreItem -Host "vcenter.lab.local" -User "admin@lab.local" -Password "MySecurePassword"
Then your script can connect silently:
Connect-VIServer -Server "vcenter.lab.local" -User "admin@lab.local"
On Linux/macOS, use Get-Credential with a prompt or environment variable workaround.
Step 5: Add Logging to Your Script
$log = "C:\Logs\VM_Report_$(Get-Date -Format yyyyMMdd_HHmm).log"
"Report started at $(Get-Date)" | Out-File $log
Get-VM | Select Name, PowerState | Export-Csv "C:\Reports\DailyVMs.csv" -NoTypeInformation
"Report complete at $(Get-Date)" | Out-File $log -Append
Use Case: Schedule Weekly Snapshot Cleanup
Create a script:
# C:\Scripts\CleanupSnapshots.ps1
Connect-VIServer -Server "vcenter.lab.local"
Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-14)} | Remove-Snapshot -Confirm:$false
Disconnect-VIServer * -Confirm:$false
Schedule weekly using Windows Task Scheduler (Sunday 3:00 AM).
Troubleshooting
| Problem | Fix |
|---|---|
| Script does not run from Scheduler | Use full path to script and include -ExecutionPolicy Bypass |
| Cron job not executing | Use absolute paths and redirect stderr to a log file |
| Credentials not accepted | Use credential store, not plain-text password in the script |
| Task fails with access denied | Check task is set to run with elevated privileges |