?
Solved

PowerShell command / script to get previous month

Posted on 2014-04-10
2
Medium Priority
?
4,929 Views
Last Modified: 2014-04-10
Q1:
I could get the current month with the command
below.  How do I get the previous month ie March ?

PS C:\Users\Art> Get-Date -format MMMM
April


Q2:
Likewise, how do I get previous & next day's date?

I'm on Win 2008 R2 and Windows 7 Profsnl : guess
it's the same PowerShell for both
0
Comment
Question by:sunhux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 39991135
With the AddMonths() and AddDays() methods, for example like this:
$Today = Get-Date
$LastMonth = $Today.AddMonths(-1) | Get-Date -Format "MMMM"
$Yesterday = $Today.AddDays(-1)
$Tomorrow = $Today.AddDays(1)

Open in new window

You can pipe any object to Get-Member (or the "gm" alias) to find the methods supported. In this case: Get-Date | gm
0
 

Author Closing Comment

by:sunhux
ID: 39991148
Wonderful
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question