How to Check for Monday in PowerShell Using an IF Statement?

0
15
Asked By CreativeCoder42 On

I'm working on a PowerShell script that needs to know the day of the week. I've got this part down: `$DoWk = (get-date $searchDate).dayofweek` and that seems to work fine. But when I try to check if it's Monday using an `if` statement like this: `if ($DoWk -ne Monday)`, I get an error that says "You must provide a value expression following the '-ne' operator." Can anyone help me figure out what I'm doing wrong? Thanks!

3 Answers

Answered By PowerShellNinja88 On

Just to add, since `$DoWk` returns an enum, a more precise way would be to use: `if ($DoWk -ne [System.DayOfWeek]::Monday)`. That way it checks against the actual enum value, which is what you're dealing with here.

Answered By ScriptingSage99 On

As others have mentioned, you definitely need quotes. Just remember that `DayOfWeek` gives you an enum, so comparing it to just 'Monday' without quotes won’t work at all.

CodeWhiz_007 -

Exactly! Remember, using the enum directly is often cleaner than string comparisons. Good call!

Answered By TechGuruX101 On

It looks like you forgot to put quotes around 'Monday'. It should be `if ($DoWk -ne 'Monday')` to treat it as a string.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.