Avatar of RandyGeorgeRedSox
RandyGeorgeRedSoxFlag for United States of America asked on

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!
Microsoft Legacy OS

Avatar of undefined
Last Comment
Steven Carnahan

8/22/2022 - Mon
Joseph Daly

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}
Steven Carnahan

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.
ASKER
RandyGeorgeRedSox

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
Steven Carnahan

Can you provide a sample of 1-5 lines of the .CSV file?  Edited for privacy of course.
ASKER
RandyGeorgeRedSox

here you go
delete1.csv
Steven Carnahan

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Steven Carnahan

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.
ASKER
RandyGeorgeRedSox

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.
ASKER CERTIFIED SOLUTION
Joseph Daly

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Steven Carnahan

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Joseph Daly

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?
Steven Carnahan

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.