HOWTO: Delete files on multiple servers


The following PowerShell script recipe will help you delete a remote file based on a list of computers stored in a text file. New PowerShell function will be created during the session which will be piped from the text file.


1. Create new file named C:\Scripts\Active_Computers.txt:

   - Create new file named C:\Scripts\Active_Computers.txt and populate the computer names.

2. Start Windows PowerShell:

   - Click Start, type PowerShell, and then click Windows PowerShell.

3. Use this script to files:

   - function delete-remotefile {
PROCESS {
$file = "\\$_\c$\install.exe"
if (test-path $file)
{
echo "$_ install.exe exists"
Remove-Item $file -force
echo "$_ install.exe file deleted"
}
}
}
Get-Content C:\Scripts\Active_Computers.txt | delete-remotefile.

4. Also you can use this script to delete files:

   - $Computerlist = get-content C:\XXXXXXXX\Servers.txt
Foreach ($computer in $Computerlist) {
Get–ChildItem –Path \\$computer\c$\XXXXXXX\*.* –Include *.* –Recurse –Force | Remove–Item –Force
{

Also consider using Action1 to delete files remotely if:
- You need to perform this action on multiple (hundreds or even thousands) computers simultaneously.
- Some of your endpoints are laptops not connected to corporate network at all times.

Action1 is a cloud-based platform for software deployment, software/hardware inventory, patch management, endpoint configuration and more. It is free with basic functionality.


Other Relevant HOWTOs: