header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Question regarding date formatting
Last Post 25 Jan 2011 03:18 PM by grayson. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
graysonUser is Offline
New Member
New Member
Posts:9
Avatar

--
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 LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    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
    graysonUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    25 Jan 2011 03:18 PM
    Shay. That worked great. Thanks for the quick response.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer