Scheduling a PowerShell Script

A Power Shell Script can be scheduled using the Windows Task Scheduler. Once you have the PowerShell script ready, you could schedule the PowerShell script by launching the Windows Task Scheduler

Screen Shot 2018-02-18 at 7.45.05 PM.png

  1. On the right click on Create Basic Task
  2. Then it would open up a small window which has three main tabs that you need to focus on-GeneralTriggers and Actions.
  3. On the General tab, select the radio button “Run whether a user is logged in or not”or otherwise depending on whether you want to run the script when the user is logged In.
  4. On the Triggers tab, specify the time/frequency when the script needs to run.
  5. On the Actions Tab, Choose the Action “Start a Program” from the Dropdown and enter PowerShell.exe. Then against the parameters specify the Path of the PowerShell script and click OK.

Now your PowerShell script would run at the scheduled time provided the system is powered ON.



Sending an email via PowerShell

Sending an email via PowerShell is pretty easy! All you need to know apart from the sender’s email address is the SMTP Server.

Here is a basic template of sending an email via Power Shell

$From = ""
$To = ""
$Cc = ""
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = ""
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) 

Finding a matching brace in PowerShell ISE

Just like in Visual Studio, it is helpful to know the matching brace in the Power Shell Editor. If you are near the opening brace, it would tell you where the closing brace is and vice-versa.

Power Shell ISE is the Power Shell Editor for Windows. In order to find the matching brace, one would have to type the following keyboard shortcut



Power Shell-Finding the date of the year

There maybe times when you need to find the date of the year that could be used in your script. There are 365 days in a year, 366 when it is a leap year.  Power Shell has a single statement that helps one find out the date of the year.

Get-Date -UFormat %j



Commenting in Power Shell

Each language has it’s own way for adding comments. Power Shell uses Hash Tags for commenting purposes.

For Example-

One can add comments to these statements in this way-





Beginning with Power Shell

Power Shell is a scripting tool from Microsoft. One can write scripts to automate processes or tasks in Windows. The boundaries of Power Shell is not confined to only Windows, Power Shell is being used in other Microsoft Technologies like SharePoint, Exchange and Office 365.

Power Shell scripts are saved with the extension ps1 . It is written in an ISE called Windows PowerShell ISE. This is where you can write scripts and test it.




To Setup Forwarding from a distribution list to an Office 365 Group

There is not a straight forward process to forward email from a distribution list to an Office 365 Group. You would need to follow these steps in order to do that-

  1. Create a Shared Mailbox.
  2. Add the newly created Shared Mailbox as a member of the distribution list. Doing so would allow the Shared Mailbox to receive email from the distribution list.
  3. Set up Email forwarding in the shared mailbox to forward email to the Office 365 Group. Now there is a point to be noted here, you would not be able to do this in the Exchange Online Admin Center. You would need to use Power Shell in Order to setup forwarding to an Office 365 Group.

Set-Mailbox shared -ForwardingSmtpAddress <EmailAddress> -DeliverToMailboxAndForward $true