Delete a group of files using UNC path contained with CSV file

i have a large group of files that I need to delete in a wide range of locations.  I have the UNC path to all of the files I need to delete in a spreadsheet, but im having a hard time using the Remove-Item cmdlet to do the job.   I discovered the following simple script to perform this task.  

$files = Get-Content "Z:\delete\delete.csv"
foreach ($file in $files) {  
    Remove-Item -Path $file -force
     }  
write-host -foregroundcolor yellow "Delete action complete"


However, when I run the script as administrator, I get the following output.  

  + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand


The files in question do exist in the specified location.  
thanks in advance for your help!
RandyGeorgeRedSoxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joseph DalyCommented:
Try this assuming you have a CSV. The first row across the top should be column headers something like UNCpath for location of the files.

import-csv c:\delete.csv | foreach {remove-item -path $_.UNCpath -force}
0
Steven CarnahanNetwork ManagerCommented:
Add a line to display what the $file contains:

$files = Get-Content "Z:\delete\delete.csv" 
foreach ($file in $files) {  
    write-host "file : " . $file
    Remove-Item -Path $file -force 
     }  
write-host -foregroundcolor yellow "Delete action complete" 

Open in new window


It sounds like it isn't getting populated the way you anticipate.
0
RandyGeorgeRedSoxAuthor Commented:
looks like the contents of the CSV file are not being pulled into the $file variable.  here's the output as the script cycles through each record.


file :  .
Remove-Item : Cannot bind argument to parameter 'Path' because it is an empty string.
At Z:\delete\delete.ps1:4 char:22
+     Remove-Item -Path <<<<  $file -force
    + CategoryInfo          : InvalidData: (:) [Remove-Item], ParameterBindingValidatio
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Mic
   veItemCommand
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Steven CarnahanNetwork ManagerCommented:
Can you provide a sample of 1-5 lines of the .CSV file?  Edited for privacy of course.
0
RandyGeorgeRedSoxAuthor Commented:
here you go
delete1.csv
0
Steven CarnahanNetwork ManagerCommented:
Is line 1 in the file actually UNCPath?  That may be the line causing the error. In that case you should try what xxdcmast stated.
0
Steven CarnahanNetwork ManagerCommented:
Alright I have started over.  Using your original code (modified to fit my environment) I have this:

$files = Get-Content "y:\delete.csv" 
foreach ($file in $files) {  
    write-host "File : "  $File
    Remove-Item $file -force 
     }  
write-host -foregroundcolor yellow "Delete action complete" 

Open in new window


Contents of delete.csv:

UNCPath
\\fs01v\home\sscarnah\test1\test1.xls
\\fs01v\home\sscarnah\test2\test2.xls
\\fs01v\home\sscarnah\test3\test3.xls

I have created the folders and files listed above in the proper location.

I then ran the script and it returned:  (I only put the first failure here)

C:\Users\sscarnah\Desktop>powershell c:\users\sscarnah\desktop\testdelete1.ps1
File :  UNCPath
Remove-Item : Cannot find path 'C:\Users\sscarnah\Desktop\UNCPath' because it d
oes not exist.
At C:\users\sscarnah\desktop\testdelete1.ps1:5 char:16
+     Remove-Item <<<<  $file -force
    + CategoryInfo          : ObjectNotFound: (C:\Users\sscarnah\Desktop\UNCPa
   th:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Remov
   eItemCommand

File :  \\fs01v\home\sscarnah\test1\test1.xls
Remove-Item : Cannot find path '\\fs01v\home\sscarnah\test1\test1.xls' because
it does not exist.
At C:\users\sscarnah\desktop\testdelete1.ps1:5 char:16
+     Remove-Item <<<<  $file -force
    + CategoryInfo          : ObjectNotFound: (\\fs01v\home\sscarnah\test1\tes
   t1.xls:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Remov
   eItemCommand

.
.
.

Delete action complete

C:\Users\sscarnah\Desktop>


It is obviously running and reading the delete.csv file - it is also reading the first line as I suspected but continues on.
0
RandyGeorgeRedSoxAuthor Commented:
so it looks like the Get-Content cmdlet is pulling in the contents of the CSV file, but Remove-Item is still unable to see the path to the file?  Looks like you've recreated the same error I am seeing.
0
Joseph DalyCommented:
Im going to post this again because it will work.

import-csv c:\delete.csv | foreach {remove-item -path $_.UNCpath -force}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steven CarnahanNetwork ManagerCommented:
results using import-csv y:\delete.csv | foreach {remove-item -path $_.UNCpath -force} as testdelete2.ps1

Drive changed to match my environment - all other items remain the same.

Y:\>powershell y:\testdelete2.ps1
Remove-Item : Cannot find path '\\fs01v\home\sscarnah\test1\test1.xls' because
it does not exist.
At Y:\testdelete2.ps1:1 char:48
+ import-csv y:\delete.csv | foreach {remove-item <<<<  -path $_.UNCpath -force
}
    + CategoryInfo          : ObjectNotFound: (\\fs01v\home\sscarnah\test1\tes
   t1.xls:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Remov
   eItemCommand


This does skip the first line of the y:\delete.csv that contains UNCPath.
0
Joseph DalyCommented:
In your original post you mention you run the script as administrator. Do you mean right click the PS1 and then select run as administrator?

If this is the case then you are probably getting the errors due to insufficient permissions. The run as administrator will run with local credentials.

Have you tried running the script as a user with privileges to all the shares?
0
Steven CarnahanNetwork ManagerCommented:
xxdcmast

My results for using your command were run under my domain admin account with sufficient privileges (especially since I had just created all the folders/shares for this test) I don't think it is permission related.  

The folder/file does exist at the location:

C:\Users\sscarnah>dir \\fs01v\home\sscarnah\test1
 Volume in drive \\fs01v\home is Data
 Volume Serial Number is B477-6138

 Directory of \\fs01v\home\sscarnah\test1

09/04/2013  03:12 PM    <DIR>          .
09/04/2013  03:12 PM    <DIR>          ..
09/04/2013  03:12 PM             8,714 test1.xlsx
               1 File(s)          8,714 bytes
               2 Dir(s)  81,875,951,616 bytes free

C:\Users\sscarnah>


**************   WOW   ****************  Pony10us = ID10T **************

I had the wrong extension on the files in the delete.csv.  I used .xls instead of .xlsx.  Once I corrected that both my version and yours worked just fine.  Mine still throws an error about the first line, UNCPath, but still does the job. Yours simply runs and then returns to the prompt.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.