2

I have scripts in both PowerShell and Batch file where in it will deleted all the files older than X days from the root folder and inside the sub folders as well from one computer, but I am looking for script where it will delete the files older than X days from multiple servers using one script.

Below is the code which I used in both PowerShell and Batch files

PowerShell Script:

Clear-Host

Set-ExecutionPolicy RemoteSigned
$Now = Get-Date
$Days = "30"

$TargetFolder = "C:\Older"
$Extension = "*.*"
$LastWrite = $Now.AddDays(-$Days)

$Files = Get-ChildItem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}


foreach ($File in $Files)
  {
  if ($File -ne $NULL)
    {
      write-host "Deleting File $File" -ForegroundColor "DarkRed"
    Remove-Item $File.FullName | out-null
    }
  else
    { Write-Host "No more files to delete!" -foregroundcolor "Green" }
  }

Batch file:

ForFiles /p "C:\Older" /s /d -30 /c "cmd /c del @file"

Pause

1 Answer 1

2

For PowerShell logic to run from one location against multiple servers, ensure Enable-PSRemoting is run on each of the servers you want to run commands against remotely. Once that is enabled, use Invoke-Command to run PowerShell commands and other executable files on remote servers.

  1. Ensure PSRemoting is enabled on each server by running Enable-PSRemoting -Force on each of the servers one time, via Group Policy or startup scripts, or however you normally do that in your environment.

  2. Create a list of the servers you want to run the command against to do the cleanup either by putting an array list in the scripted logic or in a text file and using that to create an array via get-content.

    So for the server list the two options are...

    1. Put the list of servers in the script itself $Servers = @("Server1","Server2.domain.com","Server4.otherdomain.com");
    2. Have a text file with the servers in it and use Get-Content in the script to get the list

Script

Clear-Host
Set-ExecutionPolicy RemoteSigned

$Servers = @("Server1","Server2.domain.com","Server4.otherdomain.com");
###$Servers = Get-Content "\\Server\Share\Folder\Lists\ServerCleanupList.txt"

$Now = Get-Date
$Days = "30"
$TargetFolder = "C:\Older"
$Extension = "*.*"
$LastWrite = $Now.AddDays(-$Days)

$Servers | %{
    Invoke-Command -ComputerName $_ -ScriptBlock {
        $Files = Get-ChildItem $Using:TargetFolder -Include $Using:Extension -Recurse | Where {$_.LastWriteTime -le $Using:LastWrite}
        $Files | %{If($_.FullName){$_.FullName | Remove-Item -Force}}
        C:\Windows\System32\forfiles.exe /p "C:\Older" /s /d -30 /c "cmd /c del @file"
        } -AsJob;
};

Using Invoke-Command with the -AsJob parameter executes the command on the remote system as a background job, and then moves on doing the same for each subsequent server in the list.

Supporting Resources

  • Enable-PSRemoting
  • Invoke-Command

    -ScriptBlock scriptblock

    The commands to run.

    Enclose the commands in curly braces { } to create a script block. This parameter is required.

    By default, any variables in the command are evaluated on the remote computer. To include local variables in the command, use -ArgumentList or in PowerShell 3.0+ use the prefix $using: before the local variable to be passed e.g. { echo $using:mylocalVar }

  • Get-Content

7
  • The above script which you gave is not removing the files from the folders
    – chandu
    Commented Apr 13, 2020 at 16:20
  • For the batch file is any option to get all the list of servers an d then executed the command which i have tried above
    – chandu
    Commented Apr 13, 2020 at 16:23
  • The Script is working absolutely fine as i expected, if possible can you please help me with a Batch file script
    – chandu
    Commented Apr 13, 2020 at 17:52
  • Let us continue this discussion in chat.
    – chandu
    Commented Apr 14, 2020 at 11:36
  • I have used the above to for running the script on multiple servers, i had given the server name as a FQDN but still when i run the script it is deleting the files only on the server where i run the script
    – chandu
    Commented Apr 30, 2020 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .