Scheduling a Powershell Command

Just wanted to share this with you all.

So I have a number of scripts that I schedule to run either every 10 min, once a day or once a week.  It’s simple really, I create .cmd file that I then use the task scheduler or schtasks to create. 

Inside the .cmd file I have this:

powershell c:psTest-ExchangeServers.ps1 -localhost -testsmtp

That’s it .. well almost.  If the script needs to use any exchange commands, you need to add the exchange snap in.  To do that you need to add this to your .ps1 file.  The following command check to see if the snapin is loaded and if not it loads it.  That why  if you start the Exchange Management Shell it will no try to add the already loaded snapin

$xPsCheck = Get-PSSnapin | Select Name | Where {$_.Name -Like “*Exchange*”}; If ($xPsCheck -eq $Null) {Add-PsSnapin *Exchange*}


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: