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

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: