• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

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
0
virkshivraj
Asked:
virkshivraj
  • 4
  • 2
1 Solution
 
Dale HarrisProfessional Services EngineerCommented:
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
 
Dale HarrisProfessional Services EngineerCommented:
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
 
virkshivrajAuthor Commented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Dale HarrisProfessional Services EngineerCommented:
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
 
virkshivrajAuthor Commented:
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
 
Dale HarrisProfessional Services EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now