pwsh quick tip: Check for Weekday/Weekend
So, this is a quick one. I came to the point where I had to check my script is only not running on the weekend. Since this piece of code is right after a foreach-loop in a script that is executed at some point during a workday is a basic task there are no other checks for this.
It basically comes down to integer return value of the Weekday. 1..5 is Monday..Friday and 6 is Saturday and 0 is Sunday. And this is already everything there is to this.
But Frank, what if I don’t need an if-condition but a variable with a simple bool?
As you can see, it is the same code since a if statement boils down to a simple bool condition, nevertheless I also prefer bool-variables rather than integer-variables.
To conclude: This is the road to Rome I take, there are a lot of other approaches for this. Some include regexing the DateTimeString but this limits the approach to a specific psculture. So, if this checks for the word “Monday” but is run on a Japanese computer which would return “Getsuyōbi” which not even close to the English word. Others utilize the Get-Date-Cmdlet more than once which is kind of a waste of resources since every request must be proceeded but wont change much since there are only a few lines of code in the if-statement, except you checking for a change in the millisecond scope.
I also try to optimize my code from the beginning, not necessarily over-engineer it but having at least lines of code as possible is a healthy approach in my mind.