The Surly Admin

Father, husband, IT Pro, cancer survivor

Quick Script: Date Ranges

Ever need to get an array of all the days between two dates?  This script will calculate the date range and load up an array with date/time objects for every date in between the entered times:

Function Get-DateRange
{ [CmdletBinding()]
Param (
[datetime]$Start = (Get-Date),
[datetime]$End = (Get-Date)
)
ForEach ($Num in (0..((New-TimeSpan Start $Start End $End).Days)))
{ $Start.AddDays($Num)
}
}

view raw
Get-DateRange.ps1
hosted with ❤ by GitHub

To use it, simply call the function

  • Get-DateRange
  • Get-DateRange -Start 7/24/14 -End 8/1/14
  • Get-DateRange -End 7/20/14

And just to pretty it up as a Function and with comment-based help:

Function Get-DateRange
{ <#
.SYNOPSIS
Find out all the dates inbetween the range of dates you specify
.DESCRIPTION
Simple function to return an array of date/time objects for all days inbetween the two dates
you specify.
.PARAMETER Start
Start date – this specifies the beginning of your range
.PARAMETER End
End date – this specifies the ending of your range. This date can be before OR after the Start
date.
.INPUTS
None
.OUTPUTS
[DateTime[]]
.EXAMPLE
Get-DateRange -Start 7/24/14 -End 7/1/14
Get all of the dates between the 24th to the 1st in reverse order. 24 DateTime objects will
be returned.
.EXAMPLE
Get-DateRange -End 8/1/14
Get all of the dates between today and 8/1/14. As of 7/14/14 that would be 8 dates.
.NOTES
Author: Martin Pugh
Twitter: @thesurlyadm1n
Spiceworks: Martin9700
Blog: http://www.thesurlyadmin.com
Changelog:
1.0 Initial Release
.LINK
#>
[CmdletBinding()]
Param (
[datetime]$Start = (Get-Date),
[datetime]$End = (Get-Date)
)
ForEach ($Num in (0..((New-TimeSpan Start $Start End $End).Days)))
{ $Start.AddDays($Num).Date
}
}

July 25, 2014 Posted by | PowerShell | | Leave a comment

Spicecorp Thursday, July 24th 2014

Should have posted this a couple of weeks ago, but I’ll be at the Boston Spicecorps this evening doing a quick, one hour introduction to PowerShell.  It’s all about taking some of the fear away from using PowerShell.  We’ll be going over objects and the 3 commands you have to know to really get into PowerShell:

  • Get-Command
  • Get-Help
  • Get-Member

There’s about 10-15 minutes of slides to get through, then the rest of the time is all sitting in front of the ISE and writing a script from the inside out.  If you’re not doing anything this evening and you are in the Boston area, swing by and say hello!

Link to Spicecorp Boston

July 24, 2014 Posted by | General | Leave a comment

Exporting User Information

Need to export user information to a CSV?  Perhaps as part of your termination user process?  Incredibly easy with PowerShell, let’s see how to do it.

Continue reading

July 14, 2014 Posted by | Powershell - Getting Started | , , , , | Leave a comment

Extending the Active Directory Schema

Bit of a departure from my normal PowerShell-centric posts, I want to talk about extending the Active Directory schema.  There’s some really great information on the Internet for doing this, but there are some things to consider and none of that information seems to be in one place, and I wanted to bring it together here.

Continue reading

July 7, 2014 Posted by | Technical | , , , | 1 Comment