The Multifunctioning DBA

Oct 26 2008   1:25AM GMT

Powershell Date Math

Colin Smith Colin Smith Profile: Colin Smith

Tags:
Exchange Server ActiveSync

A few years ago my company wanted me to write a script to enforce a active directory policy that dealt with user accounts. They wanted all accounts that had not been used in 90 days to be automatically disabled and after not being used for 180 days the account to be deleted. In order to do this I had to do some Date Math. This was before I was introduced to Powershell so I had to do it in VBScript. Now I am interested in Date Math using Powershell because our ITSEC officer calls me a couple times a year and questions if the script is working or not. Just so you now the script has been correct every time. Anyway, when she calls I always have to do the math on my own to determine if the script was correct in its action or inaction. I have done a Powershell script that will do this math for me. IT is a really easy script and I am sure that it will come in useful when I get called about my 90-180 script as well as some other times. It was well worth the 5 minutes that it took to sit down and write. So here we go.


############################################################################

## Get current date and add or subtract number of days and show that date ##

############################################################################

## Find out from user how many days they want to look forward or back

$date = [datetime]::Now

Echo “`nThe current date is $date`n”

$days = Read-Host “How many days? This will show you x days ago and x days from now.”

$futuredate = [datetime]::Now.AddDays($days)

Echo “`nThe date $days days from now is $futuredate”

$pastdays = -$days

$pastdate = [datetime]::Now.AddDays($pastdays)

echo “`nThe date $days days ago was $pastdate`n”

###########################################################################

As you can see this is a very short and easy script. So here is what I am doing. First I load the variable $date with the current date with the [datetime]::now. Now I go ahead and echo that out just so we know the current date. If you are like me, you are always looking to the bottom right of your start bar to find the date. Now I ask the user using the read-host cmdlet how many days back and forward that they would like to see the dates for. I load that into $days. Now I load $futerdays by using [datetime]::now.adddays($days) function. Then I can echo that date out. Then I just repeat the process by loading $pastdate with the current date added with the negative value of $days. Here is what the output of the script looks like.


PS E:\New Scripts> .\Dateplusminus.PS1

The current date is 10/25/2008 18:24:02

How many days? This will show you x days ago and x days from now.: 30

The date 30 days from now is 11/24/2008 18:24:07

The date 30 days ago was 09/25/2008 18:24:07

If you have any questions or comments about this please let me know by leaving a comment or heading over to sysadminsmith.com and sending me a email.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: