#requires -version 2.0 <# ----------------------------------------------------------------------------- Script: Get-TotalWeekDays.ps1 Version: 0.9 Author: Jeffery Hicks http://jdhitsolutions.com/blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Date: 7/31/2012 Keywords: Comments: "Those who forget to script are doomed to repeat their work." **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** ----------------------------------------------------------------------------- #> Function Get-TotalWeekDays { <# .Synopsis Get total number of week days .Description Return the number of days between two dates not counting Saturday and Sunday. .Parameter Start The starting date .Parameter End The ending date .Example PS C:\> Get-TotalWeekDays -start 7/1/2012 -end 7/31/2012 22 .Inputs None .Outputs Integer #> [cmdletbinding()] Param ( [Parameter(Position=0,Mandatory=$True,HelpMessage="What is the start date?")] [ValidateNotNullorEmpty()] [DateTime]$Start, [Parameter(Position=1,Mandatory=$True,HelpMessage="What is the end date?")] [ValidateNotNullorEmpty()] [DateTime]$End ) Write-Verbose -message "Starting $($myinvocation.mycommand)" Write-Verbose -Message "Calculating number of week days between $start and $end" #define a counter $i=0 #test every date between start and end to see if it is a weekend for ($d=$Start;$d -le $end;$d=$d.AddDays(1)){ if ($d.DayOfWeek -notmatch "Sunday|Saturday") { #if the day of the week is not a Saturday or Sunday #increment the counter $i++ } else { #verify these are weekend days Write-Verbose ("{0} is {1}" -f $d,$d.DayOfWeek) } } #for #write the result to the pipeline $i Write-Verbose "Ending $($myinvocation.mycommand)" } #end function