Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-25
6
Medium Priority
?
388 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
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 
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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…

610 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