Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question is an exact duplicate of:

If I run the script on 2/4/2013or any other day in February I want the date to be change to 1/1/2013. I am replacing date on line 59 with this value.

#Get the content of the CMS Script.
$CMSReport = Get-content C:\reports\CMSReport.acsauto

# Go to line 59 and replace the date for Last Month date.
$CMSReport[58] = $CMSReport[58] -replace "([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d", [datetime]::Today.ToShortDateString()
$CMSReport | Set-Content C:\reports\testCMS.acsauto

#Run the CMS script
Invoke-Expression -command "c:\reports\testCMS.acsauto"
share|improve this question

marked as duplicate by Chris S Feb 12 '13 at 14:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

# Todays date
$cntDate = Get-Date

# First day of current month
$firstCntMonth = Get-Date -Day 1 -Month $cntDate.Month -Year $cntDate.Year -Hour 0 -Minute 0 -Second 0

# Last day of previous month
$lastPrevMonth = $firstCntMonth.AddDays(-1)

Write-Host $lastPrevMonth

In this example I used Get-Date to polulate the $cntDate variable, you might want to construct the current date from elsewhere.

I'm sorry, without more information about where you're grabbing the date from I can't really tell you much more.. The other answer has the right idea, grab the other date, calculate the value that you want and insert the correct date rather than trying to replace in one line.

share|improve this answer

Get the value, parse it as a date, AddMonths(-1), put it back. Replace is not the way to go here.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.