none
Powershell: datetime property month error RRS feed

  • Question

  • Hi All,

    i am trying to find files created in the month of Feb 2015. here is what I have

    gci | where-object { $_.creationtime.month -eq (get-date).addmonths(-2)}

    but it doesn't work. can anyone show me why?

    Thanks

    SQL75

    Monday, April 6, 2015 12:32 AM

Answers

  • Hi,

    You're missing the .Month property in the second part of your where:

    Get-ChildItem -Recurse | 
        Where { $_.CreationTime.Month -eq (Get-Date).AddMonths(-2).Month }


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by SQL75 Monday, April 6, 2015 12:39 AM
    Monday, April 6, 2015 12:37 AM
  • I don't know of an article that goes over this exactly, but here's a bit of a walkthrough on how you could find this:

    # Get a test file from your range
    PS C:\Scripting\Testing> $a = Get-ChildItem .\2-16-2015\t1.txt
    
    # Check the CreationTime property
    PS C:\Scripting\Testing> $a.CreationTime
    
    Monday, February 16, 2015 7:57:56 PM
    
    # See what other properties are available
    PS C:\Scripting\Testing> $a.CreationTime | Get-Member -MemberType Property
    
    
       TypeName: System.DateTime
    
    Name        MemberType Definition                       
    ----        ---------- ----------                       
    Date        Property   datetime Date {get;}             
    Day         Property   int Day {get;}                   
    DayOfWeek   Property   System.DayOfWeek DayOfWeek {get;}
    DayOfYear   Property   int DayOfYear {get;}             
    Hour        Property   int Hour {get;}                  
    Kind        Property   System.DateTimeKind Kind {get;}  
    Millisecond Property   int Millisecond {get;}           
    Minute      Property   int Minute {get;}                
    Month       Property   int Month {get;}                 
    Second      Property   int Second {get;}                
    Ticks       Property   long Ticks {get;}                
    TimeOfDay   Property   timespan TimeOfDay {get;}        
    Year        Property   int Year {get;}                  
    
    # Check what the Month property looks like
    PS C:\Scripting\Testing> $a.CreationTime.Month
    2
    
    # Look at the datetime object you'll be using in the where
    PS C:\Scripting\Testing> (Get-Date).AddMonths(-2)
    
    Thursday, February 05, 2015 8:52:08 PM
    
    # Again see what other properties are available
    # Also, notice that the TypeName is the same (System.DateTime)
    PS C:\Scripting\Testing> (Get-Date).AddMonths(-2) | Get-Member -MemberType Property
    
    
       TypeName: System.DateTime
    
    Name        MemberType Definition                       
    ----        ---------- ----------                       
    Date        Property   datetime Date {get;}             
    Day         Property   int Day {get;}                   
    DayOfWeek   Property   System.DayOfWeek DayOfWeek {get;}
    DayOfYear   Property   int DayOfYear {get;}             
    Hour        Property   int Hour {get;}                  
    Kind        Property   System.DateTimeKind Kind {get;}  
    Millisecond Property   int Millisecond {get;}           
    Minute      Property   int Minute {get;}                
    Month       Property   int Month {get;}                 
    Second      Property   int Second {get;}                
    Ticks       Property   long Ticks {get;}                
    TimeOfDay   Property   timespan TimeOfDay {get;}        
    Year        Property   int Year {get;}                  
    
    # Verify that the Month property matches what you were expecting
    PS C:\Scripting\Testing> (Get-Date).AddMonths(-2).Month
    2


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by SQL75 Monday, April 6, 2015 1:01 AM
    Monday, April 6, 2015 12:59 AM

All replies

  • Hi,

    You're missing the .Month property in the second part of your where:

    Get-ChildItem -Recurse | 
        Where { $_.CreationTime.Month -eq (Get-Date).AddMonths(-2).Month }


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by SQL75 Monday, April 6, 2015 12:39 AM
    Monday, April 6, 2015 12:37 AM
  • Thanks for your answer Mike. if you know an article where this is discussed please let me know.

    Regards

    SQL75

    Monday, April 6, 2015 12:40 AM
  • I don't know of an article that goes over this exactly, but here's a bit of a walkthrough on how you could find this:

    # Get a test file from your range
    PS C:\Scripting\Testing> $a = Get-ChildItem .\2-16-2015\t1.txt
    
    # Check the CreationTime property
    PS C:\Scripting\Testing> $a.CreationTime
    
    Monday, February 16, 2015 7:57:56 PM
    
    # See what other properties are available
    PS C:\Scripting\Testing> $a.CreationTime | Get-Member -MemberType Property
    
    
       TypeName: System.DateTime
    
    Name        MemberType Definition                       
    ----        ---------- ----------                       
    Date        Property   datetime Date {get;}             
    Day         Property   int Day {get;}                   
    DayOfWeek   Property   System.DayOfWeek DayOfWeek {get;}
    DayOfYear   Property   int DayOfYear {get;}             
    Hour        Property   int Hour {get;}                  
    Kind        Property   System.DateTimeKind Kind {get;}  
    Millisecond Property   int Millisecond {get;}           
    Minute      Property   int Minute {get;}                
    Month       Property   int Month {get;}                 
    Second      Property   int Second {get;}                
    Ticks       Property   long Ticks {get;}                
    TimeOfDay   Property   timespan TimeOfDay {get;}        
    Year        Property   int Year {get;}                  
    
    # Check what the Month property looks like
    PS C:\Scripting\Testing> $a.CreationTime.Month
    2
    
    # Look at the datetime object you'll be using in the where
    PS C:\Scripting\Testing> (Get-Date).AddMonths(-2)
    
    Thursday, February 05, 2015 8:52:08 PM
    
    # Again see what other properties are available
    # Also, notice that the TypeName is the same (System.DateTime)
    PS C:\Scripting\Testing> (Get-Date).AddMonths(-2) | Get-Member -MemberType Property
    
    
       TypeName: System.DateTime
    
    Name        MemberType Definition                       
    ----        ---------- ----------                       
    Date        Property   datetime Date {get;}             
    Day         Property   int Day {get;}                   
    DayOfWeek   Property   System.DayOfWeek DayOfWeek {get;}
    DayOfYear   Property   int DayOfYear {get;}             
    Hour        Property   int Hour {get;}                  
    Kind        Property   System.DateTimeKind Kind {get;}  
    Millisecond Property   int Millisecond {get;}           
    Minute      Property   int Minute {get;}                
    Month       Property   int Month {get;}                 
    Second      Property   int Second {get;}                
    Ticks       Property   long Ticks {get;}                
    TimeOfDay   Property   timespan TimeOfDay {get;}        
    Year        Property   int Year {get;}                  
    
    # Verify that the Month property matches what you were expecting
    PS C:\Scripting\Testing> (Get-Date).AddMonths(-2).Month
    2


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by SQL75 Monday, April 6, 2015 1:01 AM
    Monday, April 6, 2015 12:59 AM
  • Awesome! Thanks Mike!

    Regards

    SQL 75

    Monday, April 6, 2015 1:01 AM