
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”
}
}
}
uslnmi
There is noticeably a bundle to learn about this. I assume you made sure nice factors in features also.
I really appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thx again
17xmkf
vvj8xn
sau8o2
eqck1x
aa94z8
hnpjtw
x6p2qn
Have you ever considered about adding a little bit more than just your articles? I mean, what you say is fundamental and all. Nevertheless imagine if you added some great photos or video clips to give your posts more, “pop”! Your content is excellent but with pics and videos, this blog could undeniably be one of the greatest in its field. Very good blog!
r2fy2r