PowerShell

Tips: Working with Date and Time in PowerShell and SharePoint List Items

It’s been a while I worked on SharePoint List(s) which is still my area of interests. In this blog post I would like to share few tips to work with date and time for a requirement.

To know the current Day of Week using PowerShell

(Get-Date).DayOfWeek

DayOfWeek is a get ‘able’ property so for our requirement we need to achieve something like we use in excel like shown below

=INT(8/11/2018-(WEEKDAY(8/11/2018,13)-1))

This returns “-3” which is start of the week day (Wednesday). I can use the same in SharePoint calculated fields like

=INT([Created]-(WEEKDAY([Created],13)-1))

Where [Created] is list item Created At value – You can very well do for any custom field like any Date and Time type.

Now, in PowerShell I can achieve the same by using the below PowerShell snippet!

-[int](Get-Date).DayOfWeek + [int][System.DayOfWeek]::Wednesday

Yes, with no doubt it’s “-3”

How this helps SharePoint power users? Its all depends on the use case. Let us cut out our exercise which is explained below

  1. For a given SharePoint list define the Week Start and Week End – Ignore the settings of the Site! We aren’t discussing about SP admin content. For now we are all just a power users.
  2. Define a custom view (I will cover this in my next blog post).

Week Start:

=[Created] – (WEEKDAY([Created],13) – 1)

Week End:

=[Created] + (7 - WEEKDAY([Created],2)

 

Leave a Reply

Your email address will not be published. Required fields are marked *