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.
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.
Write-Output "The friday of the given date is $dateFriday"
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
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
This gives us a value representing the day of the week; What we need to do now is programmatically determine the number of days between this date and the Friday of that week. That?s something we can do with a single line of code:
$intadder = 5 - $intDay
Confused? Don?t be; this actually makes sense. If you refer to the table you?ll see that Friday has a value of 5. If we take the value for Friday (5) and subtract the value for given date we will get the days to add to get Friday. That?s what we do with this line of code:
$dateFriday = $date.AddDays($intddder)
System.DateTime is a powerful class that allows you to do date manipulation very easily in PowerShell
Hey, PowerShell! Given a specific date, how can I determine the beginning and ending date of the previous month? In other words, given 8/11/2005, I need to get back 7/1/2005 and 7/31/2005.
Powershell provides access to .Net framework. We can use .Net framework DateTime class to solve the problem. Let's write a script to solve this.
First, we need to get the date for which we need to determine the beginning and ending date of previous month. We can do this using PowerShell Param statement
Param ([System.Datetime] $date = $(get-date))
In the above statement, we are declaring a parameter of type Syste.DateTime. We are also initializing it to current date when no parameter is specified. It works as follows. Let's say that we have saved the above as script file BeginEndPrevMonth.ps1 then
.\BeginEndPrevMonth.ps1 11/18/2006
will set the $date variable to 11/18/2006. However, if you don't specify the parameter like
.\BeginEndPrevMonth.ps1
then PowerShell parser will execute the $(get-date) which will get you the current date and time and this value gets assigned to $date.
Let's move on. Now that we have a date, we need to find the beginning and endind date of the previous month of this date. Let's start the end date. if I have a date like 8/11/2005, then I find out the last date of previous month by subtracting the number of days in the current date. In the example, if I subtract 11 then I will get 7/31/2005. We can accomplish this in the script as follows
First, determine the number of days in given date
$numdays = $date.Day
Once we have determined the number days, we need to subtract this from the current date to get the last date of previous month. There is no subract method available in Datetime class. however, there is a AddDays method. We can use this method to subtract days by passing in a negative value to add.
$prevmonthlastday = $date.AddDays(- $numdays)
We are calling AddDays method in DateTime with - $numdays to subtract the number of days. We use the similar logic to subtract the number of days from $prevmonthlastday to determine that last day of previou to previous month and then add 1 day to get the first date.