How Can I Determine the Day of the Week?


Hey, PowerShell! I have a script that does certain management tasks based on the day of the week. I know how get the date in a script, but how can I tell whether it’s a Monday or a Tuesday or whatever?

This is very easy to do with Syste.Datetime class.

param ([system.datetime]$date = $([system.datetime]::now))
$dayofweek = $date.DayOfWeek
$intday = [int] $date.DayOfWeek
Write-Output "The day of given date is $dayofweek  and numerical value of day is $intday"

Let's see how this script works  The script begins by assigning the date from commandline or current date  to a variable named $date.

Next we use the DayofWeek  property to determine the day of the week for given date  DayofWeek is going to return one of the following value .  This is [System.DayOfWeek]  enum class values.

0      Sunday

1      Monday

2      Tuesday

3     Wednesday

4     Thursday

5      Friday

6      Saturday

if you want the numerical value of this enum, you can convert it to integer by using the [int] type converter.

Skip to main content