Using Powershell, how do you get a weekday number?

Using a Powershell date object, how do you get the day of the week, 0-6, where 0 will be Sunday and 6 will be Saturday. I know that I can get the name of the day with the code below, but how can I get the number because there is no DayNumberOfWeek or equivalent property?

(Get-Date).DayOfWeek 

I believe that I could use the day name from the above code in the switch statement to convert it to a number, but that doesn't seem very eloquent.

+6
source share
3 answers

like this:

 ( get-date ).DayOfWeek.value__ 

I suggest in the future to investigate what properties an object does in this way:

 ( get-date ).DayOfWeek | gm -f # gm is an alias for get-member 
+10
source

Well, the DayOfWeek property for DateTime is not a string, but an enumeration of DayOfWeek, so the shortest answer is probably

 [Int] (Get-Date).DayOfWeek # returns 0 through 6 for current day of week 

or

 [Int] [DayOfWeek] "Wednesday" # returns 3 

Bill

+16
source
 Get-Date -UFormat %u 

will return the generated date.

check out http://technet.microsoft.com/en-us/library/hh849887.aspx for more fomats

+3
source

All Articles