delete files from specific folder

This topic contains 1 reply, has 1 voice, and was last updated by  Arik Cher 5 months, 3 weeks ago.

  • Author
    Posts
  • #69313

    Arik Cher
    Participant

    I want to delete all the files under "W3SVC*" folder , from list of servers .

    for the import servers I used :
    Get-ADComputer -filter * -searchbase "OU=Servers,OU=Apps1,DC=QA,DC=com" | select – ExpandProperty Name | out-file C:\LM.txt

    +
    $servers =Get-Content -Path "C:\LM.txt"

    for the files deletion I used :

    $MyPath=(Get-ChildItem -path d:\ -Filter "W3SVC*" -Recurse -Directory).Fullname
    Get-ChildItem -Path $MyPath -Recurse -Force | Where-Object { !$_.PSIsContainer} | Remove-Item -Force

    How can I run the 2 last rows only on the servers in "C:\LM.txt".

    (or how can I run the whole thing more efficiently)

  • #69316

    Arik Cher
    Participant

    I added

    Invoke-Command -ComputerName $servers -ScriptBlock {
    $MyPath=(Get-ChildItem -path d:\ -Filter "W3SVC*" -Recurse -Directory).Fullname
    Get-ChildItem -Path $MyPath -Recurse -Force | Where-Object { !$_.PSIsContainer} | Remove-Item -Force
    }

    so you can ignore the post

You must be logged in to reply to this topic.