locked
I want filter Get-ADComputer -Properties PasswordLastSet by date. RRS feed

  • Question

  • I have a functional script that grabs the Get-ADComputer -Properties PasswordLastSet and displays the computer name and date. What I want is to filter out systems by the date, so I only see systems 30 days older than the date run. Here is what I have that works:

    $computers = get-content C:\Scripts\computerlist.txt
    ForEach ($comp in $computers)
    {
    Get-ADComputer $comp -Properties PasswordLastSet | Select-Object Name,PasswordLastSet | Export-CSV C:\Scripts\PasswordLastSet.csv -append -force -NoTypeInformation
    }

    How do I filter based on the results (date) of PasswordLastSet?


    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!

    Wednesday, June 12, 2013 9:23 PM

Answers

  • Use where-object:

    computers = get-content C:\Scripts\computerlist.txt
    ForEach ($comp in $computers)
    {
    Get-ADComputer $comp -Properties PasswordLastSet | Where {$_.Passwordlastset -ge (Get-date).AddDays(-30)}| Select-Object Name,PasswordLastSet | Export-CSV C:\Scripts\PasswordLastSet.csv -append -force -NoTypeInformation
    }


    Hope that helps! Jason

    • Marked as answer by zperryz Wednesday, June 12, 2013 9:31 PM
    • Unmarked as answer by Andy Qi Thursday, June 13, 2013 6:57 AM
    • Marked as answer by Andy Qi Thursday, June 13, 2013 6:57 AM
    Wednesday, June 12, 2013 9:27 PM
  • I'm not sure if you meant password last set inside or outside of the last 30 days.

    For only systems with values older than 30 days use -le

    For only systems with values Youger than 30 days use -ge


    Hope that helps! Jason

    • Marked as answer by zperryz Wednesday, June 12, 2013 9:31 PM
    Wednesday, June 12, 2013 9:29 PM

All replies

  • Use where-object:

    computers = get-content C:\Scripts\computerlist.txt
    ForEach ($comp in $computers)
    {
    Get-ADComputer $comp -Properties PasswordLastSet | Where {$_.Passwordlastset -ge (Get-date).AddDays(-30)}| Select-Object Name,PasswordLastSet | Export-CSV C:\Scripts\PasswordLastSet.csv -append -force -NoTypeInformation
    }


    Hope that helps! Jason

    • Marked as answer by zperryz Wednesday, June 12, 2013 9:31 PM
    • Unmarked as answer by Andy Qi Thursday, June 13, 2013 6:57 AM
    • Marked as answer by Andy Qi Thursday, June 13, 2013 6:57 AM
    Wednesday, June 12, 2013 9:27 PM
  • I'm not sure if you meant password last set inside or outside of the last 30 days.

    For only systems with values older than 30 days use -le

    For only systems with values Youger than 30 days use -ge


    Hope that helps! Jason

    • Marked as answer by zperryz Wednesday, June 12, 2013 9:31 PM
    Wednesday, June 12, 2013 9:29 PM
  • It's useful to look at the results of sending your query to get-member, the PasswordLastSet property is a datetime object, so it can filter against datetime objects.  If it happened to be a string value you could always cast it to a datetime object.

    [datetime]$_.Passwordlastset


    Hope that helps! Jason

    Wednesday, June 12, 2013 9:31 PM
  • Did I ever tell you're my hero? You are the wind beneath my powershell.

    Get-ADComputer $comp -Properties PasswordLastSet | Where {$_.Passwordlastset -le (Get-date).AddDays(-30)} | Select-Object Name,PasswordLastSet | Export-CSV C:\Scripts\whatever.csv -append -force -NoTypeInformation

    Worked like a charm!


    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!

    Wednesday, June 12, 2013 9:33 PM
  • Well that's very nice to say!  I'm just having fun helping out.  You should look for a local or remote powershell user group, it's a great way to find more powershell people that are looking to help you learn.

    Hope that helps! Jason

    Wednesday, June 12, 2013 10:30 PM