HOWTO: Delete Files on Multiple Servers Using Powershell Script

PowerShell is an object-oriented software engine and a command-line scripting language that provides IT professionals with greater opportunities to configure the operating systems of the MS Windows family. Simply put, it is a kind of universal administration tool. This article will discuss the basic techniques for writing scripts on PowerShell, allowing you to automate the management of your Windows environment in a simple way.

PowerShell offers both a pure console interface and a full-fledged PowerShell ISE (Integrated Scripting Environment) integrated scripting environment. To launch the command line interface, enter powershell in the Run menu (WinKey + R). PowerShell ISE is launched using the “PowerShell ISE” command in the same menu.

ISE is more preferable, because it provides more opportunities for the developer due to syntax highlighting, code auto-completion function and other features inherent in many “big” IDEs.

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

Using the keyboard, you should quickly launch "Windows Explorer." You just need to press the keys Win + E and "Explorer" will be in front of your eyes.

Create new file directory C:\Scripts\ and file named Active_Computers.txt in this folder and populate the computer names.

Step 1 to Delete Files on Multiple Servers Using Powershell Script is to create new file directory

Create new file

2. The Next Step - Start Windows PowerShell

Click Start, type PowerShell, and then click Windows PowerShell. If Windows 10 is installed on your computer, then perhaps an even faster way to open PowerShell is to right-click on the “Start” button and select the desired menu item (there are two items at once - for easy launch and on behalf of the administrator). The same menu can be accessed by pressing the Win + X keys on the keyboard.

Start Windows PowerShell

3. Use This Script to Delete Files

In the PowerShell window that appears, enter the following code:
  • function delete-remotefile {
  • $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.

    Use this script

    4. Also You Can Use This Script to Delete Files

    You can find another script for deleting files below. The procedure is the same as the previous one.
    • $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: