?
Solved

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

Posted on 2011-09-25
6
Medium Priority
?
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

 
LVL 16

Accepted Solution

by:
Dale Harris earned 2000 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

777 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