Register
: :
Login
Home
News
Forums
Scripts
User Groups
Resources
About
Forums
Search
Members
Unanswered
Active Topics
Forums
>
PowerShellCommunity.org
>
Community Announcements and Assistance
Question regarding date formatting
Last Post 25 Jan 2011 03:18 PM by
grayson
. 2 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
grayson
New Member
Posts:9
25 Jan 2011 12:13 PM
I'm attempting to perform actions on files created in the last month.
I'm attempting to construct a variable for the file name pattern that has the year and month as the prefix in the format: yyyyMM.
$lastmonth = ((Get-Date).AddMonths(-1))
returns
12/25/2010 3:43:19 PM
I then need to format that into:
201012
$x = Get-Date -Format "yyyyMM" results in the format I need but of course returns the current year and month:
201101
Adding the -format option on the original command:
$lastmonth = ((Get-Date).AddMonths(-1)) -format "yyyyMM"
results in the folowing error:
You must provide a value expression on the right-hand side of the '-f' operator. At :line:1 char:43 + $lastmonth = ((Get-Date).AddMonths(-1))-fo <<<< rmat "yyyyMM"
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
25 Jan 2011 12:17 PM
The format parameter belongs to the cmdlet, you trying to use it against the method (addMonths), try the ToString() method:
(Get-Date).AddMonths(-1).ToString('yyyyMM')
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
grayson
New Member
Posts:9
25 Jan 2011 03:18 PM
Shay. That worked great. Thanks for the quick response.
You are not authorized to post a reply.
Using PowerShell
--General PowerShell
--Active Directory
--Exchange Server
--Lync Server
--SharePoint
--SQL Server
--System Center
--Non-Microsoft Products
--Books, Tools, and Videos
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Forums
>
PowerShellCommunity.org
>
Community Announcements and Assistance
Active Forums 4.3
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2