1

I am currently learning how to use powershell and was wondering how would you make a simple process like this for example.

If the time is between 9am and 12pm then write to 'Get coffee' else write nothing.

4
  • In the powershell console type: Get-Help about_if Commented May 16, 2013 at 19:48
  • possible duplicate: stackoverflow.com/questions/7631564/… Commented May 16, 2013 at 20:00
  • 1
    Hi @lolbol, welcome to stack overflow you will notice from the short responses, that people expect questions to show what you have tried, so at least we can all see some effort put in. The more you put into the question, the more you will get out. Commented May 16, 2013 at 20:25
  • I usually refer people to whathaveyoutried dot com :) Commented May 17, 2013 at 17:52

3 Answers 3

2

There are a lot of ways to do this.

if ((9,10,11) -contains (get-date).Hour) { "get coffee" }

if ((get-date).hour -ge 9 -and (get-date).hour -le 11) { "get coffee" }

if (((get-date -hour 12 -min 0 -sec 0) - (get-date)).hours -in 0..2) { ... }

This might be a fun community wiki question.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think this question needs any more answers, but here is another option: if((Get-Date).Hour -match '9|10|11|12'){'Get-Coffee'}
1

This is about as short as I can get it...

if(Get-Date | ? {($_.hour -ge 9) -and ($_.hour -le 12)}){"get coffee"}

4 Comments

Technically that will evaluate true all the way up to 12.59. You really want -le 11, to go to 11.59.
Well, it complies to exactly what the OP stated as the problem definition. There is some ambiguity in the problem definition though since the 9-12 could be minutes or hours.
Read the question again... hahahaha, sorry. It needed clarification.
this is not the stack exchange site Programming Puzzles & Code Golf, shortening code through the use of abbreviated aliases and lack of formatting is usually not simpler for beginners as the OP actually asked for.
0

My 2 cents (using .net):

[datetime]$now = [datetime]::now.Tostring("T",[System.Globalization.CultureInfo]::InvariantCulture )
$low = [datetime] "09:00:00 am"
$hi = [datetime] "12:00:00 pm"
if ( $now -ge $low -and $now -le $hi  ) { "Coffee Time!" } else {"Work!"}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.