Foreach remove-item help

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Kelly Farrell Kelly Farrell 1 year, 9 months ago.

  • Author
    Posts
  • #26895
    Profile photo of Kelly Farrell
    Kelly Farrell
    Participant

    So I don't have to keep pulling from a txt file that I create by export servers since I want this to run every 30 days.

    $computers = Get-ADComputer – SearchBase 'OU=Test,DC=Test,DC=COM' -filter * | select -expand name
    ForEach($computers in $computers)
    {
    Remove-Item c:\windows\ccmcache\* -Force -recure

  • #26896

    I guess you want to do something along the lines of

  • #26899
    Profile photo of Kelly Farrell
    Kelly Farrell
    Participant

    Thank you that worked. I so appreciate the help I was trying all different ways and none was working.

  • #26900
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    The C$ is missing from the path. You will need to possibly escape the $ with a backtick (`) like \C`$\ or I prefer a string format:

    $computers = Get-ADComputer –SearchBase 'OU=Test,DC=Test,DC=COM' -filter * 
     ForEach($computer in $computers) {
        $path = "\\{0}\c$\windows\ccmcache\*" -f $computer.name
        Remove-Item $path -Force -recure
     }
    
  • #26904
    Profile photo of Kelly Farrell
    Kelly Farrell
    Participant

    yep I added that to the path and it's working for me now, thanks

You must be logged in to reply to this topic.