Solved

How to delete files from a list of cmputers provide via csv file to powershell

Posted on 2011-09-25
6
360 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:virkshivraj
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36596951
Sounds pretty straight forward:

#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 is actually C:\Directory on the local computer.

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
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36596957
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
0
 

Author Comment

by:virkshivraj
ID: 36597037
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

$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
}

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 total points
ID: 36598750
Regarding it not working by deleting the folder, the reason is above but I'll paste it here as well: "Also, you'll need to remove the -whatif tag if you want to actually delete files."

About the computers not going through one by one, maybe your CSV field is named something different?  Can you verify it's Name?

Also, to make this simple, just do the following by working with the CSV file and script:

$Computers = Import-CSV "C:\Videos.csv"

foreach ($PC in $Computers)
{
write-host $PC.name
}

See if that shows you each PC in your list.

HTH,

Dale Harris
0
 

Author Comment

by:virkshivraj
ID: 36602420
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
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

Open in new window

0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36602594
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
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article will help you understand what HashTables are and how to use them in PowerShell.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now