Script to remove folders older then 90 days

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Brian Bourque Brian Bourque 2 years ago.

  • Author
    Posts
  • #20892

    hello again,
    I am working on a script to remove some out of date / unused user profiles from a system the script I am trying to get the work is:

    $profile = get-childitem -path c:\users -name *old* | where {((get-date)-$_.lastwritetime).Days-ge 90}
    foreach(P in $profile) {write-output $P "Found"

    how ever I am having an issue

    issue 1
    unless I put in the exact file name it will give an error "can not find overload for "op_subtraction" and the argument count: "2" – from what I can tell this is because ti is not returning results but I know for a fact there is at least one valid result. I have tried with old *old *old* "*old*" and '*old*' and get no results but if I put in the full name of the folder it returns a result

    I have seen examples like this before so I am kind of confused as to what I am doing wrong
    BTW I am using PS 2.0

  • #20893
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    Try the -Filter paramter instead of -Name

    $Profile = Get-ChildItem -Path c:\users -Filter *old* | where { ((Get-Date)-$_.LastWriteTime).Days -ge 90 }
    $Profile.FullName | % { "$_ Found" }
    
  • #20909

    yes that seems to work Thanks Sam

You must be logged in to reply to this topic.