virkshivraj
asked on
How to delete files from a list of cmputers provide via csv file to powershell
Hi All
I am trying to build a script that will fetch computer names from a csv file and delete certain files from those computers.
For instance computers.csv is the file where computer names are kept in column one of the file.
The scripts reads second row in .csv file and picks pc1 to delete "C:\Videos" folder from the computer.
Once this is done it reads row three column one from computers.csv file, picks pc2 and performs the same process and then proceeds to the next row....
I need to do this for 500 plus computers .. or else I have to skip gym
Thanks
test.jpg
I am trying to build a script that will fetch computer names from a csv file and delete certain files from those computers.
For instance computers.csv is the file where computer names are kept in column one of the file.
The scripts reads second row in .csv file and picks pc1 to delete "C:\Videos" folder from the computer.
Once this is done it reads row three column one from computers.csv file, picks pc2 and performs the same process and then proceeds to the next row....
I need to do this for 500 plus computers .. or else I have to skip gym
Thanks
test.jpg
One note: I initially included a directory column in there as well for testing purposes. This isn't actually needed. If you want to add that in later, it will not be hard at all.
Also, if you only have one field, a text file with just a computer name on each line will do just fine. This makes it so you can do this:
gci \\$PC\\C$\Videos -recurse | remove-item /force -whatif
Not too much code saved, but makes it simple.
HTH,
Dale Harris
Also, if you only have one field, a text file with just a computer name on each line will do just fine. This makes it so you can do this:
gci \\$PC\\C$\Videos -recurse | remove-item /force -whatif
Not too much code saved, but makes it simple.
HTH,
Dale Harris
ASKER
Hi Dale
Output time .... i have added write-host \\$($PC.name)\C$\test to the code .. it tells me what the script is spitting out .. but for some reason it does not delete the folder and it dose not even go to the second pc2 ..
here is a sample output
PS C:\> D:\Scripts_Test\Videos.ps1
\\PC1\C$\test
Output time .... i have added write-host \\$($PC.name)\C$\test to the code .. it tells me what the script is spitting out .. but for some reason it does not delete the folder and it dose not even go to the second pc2 ..
here is a sample output
PS C:\> D:\Scripts_Test\Videos.ps1
\\PC1\C$\test
$Computers = Import-CSV "C:\Videos.csv"
foreach ($PC in $Computers)
{
write-host \\$($PC.name)\C$\test
gci \\$($PC.name)\C$\test | remove-item -recurse /force -whatif
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i have changed the scope of the initial request now .. now instead of csv file i am using a text file ...
see code Snippet for that one the following code works ...
But when I try to do the same for shared drives it is a no-no situation .. the reason why i need it is because I want o delete some files from user profile in a domain environment
so the way i have done is by providing the name of the users in a text file and I want the "$_" to pick the username (which is actually a folder name) from the txt file
see the code below
#MAIN
function delete-remotefile {
PROCESS {
$file4 = "\\netapp1\userdata\Users\ $_\test4"
if (test-path $file4)
{
echo "$_ File exists"
Remove-Item $file4 -force
echo "$_ Files deleted"
}
}
}
# Reads list and pipes to function
Get-Content D:\CSV_Files\users.txt | delete-remotefile >> D:\CSV_Files\Log.txt
see code Snippet for that one the following code works ...
But when I try to do the same for shared drives it is a no-no situation .. the reason why i need it is because I want o delete some files from user profile in a domain environment
so the way i have done is by providing the name of the users in a text file and I want the "$_" to pick the username (which is actually a folder name) from the txt file
see the code below
#MAIN
function delete-remotefile {
PROCESS {
$file4 = "\\netapp1\userdata\Users\
if (test-path $file4)
{
echo "$_ File exists"
Remove-Item $file4 -force
echo "$_ Files deleted"
}
}
}
# Reads list and pipes to function
Get-Content D:\CSV_Files\users.txt | delete-remotefile >> D:\CSV_Files\Log.txt
MAIN
function delete-remotefile {
PROCESS {
$file1 = "\\$_\c$\test"
$file2 = "\\$_\c$\test1"
$file3 = "\\$_\c$\test2"
if (test-path $file1,$file2, $file3)
{
echo "$_ File exists"
Remove-Item $file1 -force
Remove-Item $file2 -force
Remove-Item $file3 -force
echo "$_ Files deleted"
}
}
}
# Reads list and pipes to function
Get-Content D:\CSV_Files\Computers.txt | delete-remotefile >> D:\CSV_Files\Log.txt
virkshivraj,
Things have gotten busy at work recently. Can I recommend closing this question out and assigning points if it met your previous requirements and starting a new question? This will give your question more visibility to the experts that are more available than myself. The question was answered and this is a new question.
Respectfully,
Dale Harris
Things have gotten busy at work recently. Can I recommend closing this question out and assigning points if it met your previous requirements and starting a new question? This will give your question more visibility to the experts that are more available than myself. The question was answered and this is a new question.
Respectfully,
Dale Harris
#Given two fields: Name and Directory (Directory is in format of C$\directory)
$Computers = Import-CSV ".\Computers.csv"
foreach ($PC in $Computers){
gci \\$($PC.name)\\C$\Videos -recurse | remove-item /force -whatif
}
Remember, if you want to remove files from a remote computer, you'll need to use the C$ format. Example:
\\192.168.1.1\c$\directory
Also, you'll need to remove the -whatif tag if you want to actually delete files.
Warning: NOT TESTED
Didn't have a remote computer to test it on until I go to work tomorrow.
HTH,
Dale Harris