How to Delete Files on Remote Computers, if there is need to delete files on multiple Windows machines then we have to use a script, here in this article we explain how simple code deletes files on remote computers.
It is easy to delete the files on the remote computers using Powershell, there is a command remove-item deletes files, folders, variable, registry keys, and aliases, let’s learn more about this command with examples.
Remove-Item
Syntax and Example
Examples:
Remove-Item -path "c:\temp\raj.txt"
The above example removes the raj.txt file in C:\temp drive.
Remove-Item -Path C:\temp\* -Force
The above example removes all the files under C:\temp including hidden ready only files as we declared the parameter -force at the end.
Remove-Item -Path C:\app\download\* -Force -Recurse
The Above example removes all files under C:\app\download including subfolders and files recursively as we defined the parameter -Recurse.
Powershell Script to Delete Files on Remote Computers
When you have a requirement to Copy files on multiple remote machines, then we need to write the script this way.
We need to create a file for storing all the servers or multiple machines list, I named it as Allservers.txt for and saved it in my local machine c:\temp\Allserver.txt
#remote machines list $machines= Get-Content -Path "C:\temp\AllServers.txt" foreach ($onemachine in $machines) { Write-Host "Currently the script is copying files on" $server Remove-Item -Path "\\sourcemachine\c$\softwares\adobe" -Force -Recurse }
The Above example script removes all files under C:\softwares\adobe including subfolders and files recursively on the listed remote computers in the file allservers.txt as we defined the parameters -Recurse and -force it doesn’t ask user confirmation before deleting the files.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Remove-Item Parameters
-Force
This parameter forces the command to restart a computer without asking the user confirmation.
-Recurse
This parameter is used for deleting the subfolders and files under a defined destination folder or location.
-WhatIf
This parameter shows what happens when you execute a command, I strongly recommend testing this before you restart multiple computers at once.
-Confirm
This parameter prompts user confirmation before it restarts the computer.
-Credential
If you want to restart computers using different accounts or credentials then we have to use this parameter, basically, it runs with default current user account.
Thank you for reading this article, please give me your valuable feedback by commenting.
Leave a Reply Cancel reply