Foreach remove-item help

This topic contains 4 replies, has 3 voices, and was last updated by  Kelly Farrell 2 years, 8 months ago.

  • Author
  • #26895

    Kelly Farrell

    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

    Kelly Farrell

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

  • #26900

    Rob Simmers

    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 $
        Remove-Item $path -Force -recure
  • #26904

    Kelly Farrell

    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.