Delete files based on the list of computers

Delete Files from Remote computers

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.

############################## Script – 1###########################################

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\Serverlist.txt| delete-remotefile

 

 

#############################Script – 2 ###########################################

function delete-remotefile1 {
PROCESS {
$file = “\\$_\c$\Common Folders”
if (test-path $file)
{
echo “$_ file exists”
Remove-Item $file -force -recurse
echo “$_ file deleted”
}
}
}

function delete-remotefile2 {
PROCESS {
$file = “\\$_\c$\Profiles”
if (test-path $file)
{
echo “$_ file exists”
Remove-Item $file -force -recurse
echo “$_ file deleted”
}
}
}

Get-Content  C:\Users\sfa\Desktop\PCList.txt | delete-remotefile1 | delete-remotefile2

 

#########################Script – 3 ############################################

#Creating an array of your folders
$filelist = @(” \c$\Common Folders”, “\c$\Profiles”)
#Storing file content as computerlist
$computerlist = Get-Content  C:\Users\sfa\Desktop\PCList.txt
#We go for a loop for every computer…
foreach ($computer in $computerlist)
{
Write-Host -ForegroundColor Yellow “Analysing $computer”
#And for every file in the filelist
foreach ($file in $filelist)
{
#We recreate the UNC filepath
$newfilepath = Join-Path “\\$computer\” “$filelist”
if (test-path $newfilepath)
{
Write-Host “$newfilepath file exists”
try
{
#We try to remove…
Remove-Item $newfilepath -force -recurse -ErrorAction Stop
}
catch
{
#And there will be an error message if it fails
Write-host “Error while deleting $newfilepath on $computer.`n$($Error[0].Exception.Message)”
#We skip the rest and go back to the next object in the loop
continue
}
#if SUCCESS!!1!1! …
Write-Host  “$newfilepath file deleted”
}
}

}

17 Comments

  1. I would like to express some appreciation to this writer just for rescuing me from such a circumstance. As a result of looking out throughout the internet and obtaining techniques which are not pleasant, I believed my entire life was gone. Existing without the presence of approaches to the difficulties you have fixed by means of your main review is a critical case, as well as those which may have in a negative way damaged my entire career if I hadn’t come across your web site. Your actual expertise and kindness in controlling every item was crucial. I don’t know what I would have done if I hadn’t encountered such a solution like this. It’s possible to now look ahead to my future. Thanks very much for this skilled and results-oriented help. I will not hesitate to propose your blog to any individual who will need care on this situation.

  2. Thank you, I’ve just been searching for info approximately this subject for ages and yours is the greatest I have discovered so far. However, what about the bottom line? Are you sure concerning the source?

  3. I am not sure where you’re getting your information, but great topic. I needs to spend some time learning much more or understanding more. Thanks for great information I was looking for this information for my mission.

  4. Good blog! I really love how it is simple on my eyes and the data are well written. I am wondering how I might be notified whenever a new post has been made. I’ve subscribed to your feed which must do the trick! Have a nice day!

Leave a Reply

Your email address will not be published.