Newbie... Need help with a simple delete folder on numerous machines Powershell script.

已答复 Newbie... Need help with a simple delete folder on numerous machines Powershell script.

  • Friday, May 14, 2010 3:03 PM
     
     

    Hi, I am very new to Poweshell I took a three day class, but have never scripted before except for a little batch file work. I just came across something that I thought I could use for my first attempt at a script.

    I need to delete a Folder and all of it's subcontents on numerous machines. I know about the cmdlet remove-item, the path to the folder and the -force and -recurse to deleet ecerything without prompts. What I need to know is how to have a text file with all of my IPs in it and have the script refer to each IP and then run this script on each machine outputing the results to another txt file.

    I have searched in vain for the answer, so I thought this site may get me going. Thanks for any and all input.

    • Changed Type capperdog Friday, May 14, 2010 3:53 PM
    •  

All Replies

  • Friday, May 14, 2010 3:12 PM
     
     Proposed Answer

    Do you have access to the remote machine folders? I mean can u access that using something like C$ or a read-write Share?

    If so, here is a simple solution

    Put all your IPs in a text file and name it anything you want. For this example, we will call it ips.txt

    Now, run this command

    Get-Content ips.txt | % {Remove-Item "\\$_\Share\Folder" -recurse -Force; Out-File -InputObject "Removed files from $_" -Append -FilePath C:\Scripts\out.txt}

    This shoud do the job.


    Ravikanth
    http://www.ravichaganti.com/blog
    Twitter: @ravikanth
    PowerShell 2.0 remoting - eBook
  • Friday, May 14, 2010 4:04 PM
     
     

    Thanks a bunch. I am looking up all of your syntax and writing this out. One question, after the -force you have a ; 

    Can I leave that off and start a second line of code here?

    Thaks for your help!

  • Friday, May 14, 2010 4:05 PM
     
     
  • Friday, May 14, 2010 4:09 PM
     
     
    Great working with it now!
  • Friday, May 14, 2010 4:27 PM
     
     

    FANTASTIC!!! After a few errors were cleaned up it ran perfecrly. Only thing is it only wrote Removed Files From in the text file. It did not specify the machine. I thought the $_ would make it specify the machine??

     

  • Friday, May 14, 2010 4:58 PM
     
     Answered

    Yes. It should. I just executed this on my machine and I see the IPs in out.txt

    PS C:\Scripts> gc .\ips.txt | % { Write-Host "\\$_\Share\Folder"; Out-File -InputObject "removed files from $_" -Append
    -FilePath C:\Scripts\out.txt }


    Ravikanth
    http://www.ravichaganti.com/blog
    Twitter: @ravikanth
    PowerShell 2.0 remoting - eBook
    • Marked As Answer by capperdog Friday, May 14, 2010 6:44 PM
    •  
  • Friday, May 14, 2010 6:44 PM
     
     

    Ahhh... I did not close the brackets after the second line of code.

    Thank you sir! That is my first successfull code written!

  • Friday, May 14, 2010 6:49 PM
     
     
  • Monday, June 07, 2010 3:29 PM
     
     

    Ravikanth, I need to add to this script the removal of a dll file. I worked with it quite a bit last week, but can't seem to be getting anywhere. I don't want to make the script check my text file twice for the same machine, so can you point me in the right direction? Here is the script I am trying to get to run. I know it's not right, but here it is anyway.

    Get-Content "C:\flexnet\machines.txt" | % {remove-item "\\$_\C$\Program Files\Common Files\installshield" -recurse -force
    {remove-item "\\$_\C$\Windows\Downloaded Program Files\isusweb.dll" -force}
    out-file -inputobject "Removed Files From $_" -append -filepath C:\Flexnet\results.txt}

    It will remove the installshield folder, but leaves the isusweb.dll behind. Isusweb.dll is a hidden file, but using the -force shouls delete it. I must be making a newbie mistake here somewhere.

    Thanks!


    Capperdog