Hi all,<\/p>\n
I have a list of directories in a CSV file that I am needing to delete. I am using the following script:<\/p>\n
$csv = Import-Csv C:\\temp\\test.csv\nforeach ($line in $csv) {\n Remove-Item -Path $line -Recurse -Force\n}\n<\/code><\/pre>\n
Advertisement
Here is an extract of the CSV file:<\/p>\n
C:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Montegrande\\2007 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Montegrande\\2008 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Montegrande\\2009 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Montegrande\\2010 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Z - Clients\\Z - Croftby\\2007 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Z - Clients\\Z - Croftby\\2008 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Z - Clients\\Z - Croftby\\2009 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Z - Clients\\Z - Croftby\\2010 Audit\n\n<\/code><\/pre>\nWhen I run the script, I get the following error:<\/p>\n
Remove-Item : Cannot find drive. A drive with the name '@{C' does not exist.\nAt line:3 char:5\n+ Remove-Item -Path $line -Recurse -Force\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : ObjectNotFound: (@{C:String) [Remove-Item], DriveNotFoundException\n + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand\n\n<\/code><\/pre>\nAny advice/help would be great.<\/p>\n
Thanks in advance,<\/p>\n
Dean.<\/p>","upvoteCount":7,"answerCount":9,"datePublished":"2015-11-12T01:11:46.000Z","author":{"@type":"Person","name":"deanoneill","url":"https://community.spiceworks.com/u/deanoneill"},"acceptedAnswer":{"@type":"Answer","text":"
Take a closer look at what you are trying to do:<\/p>\n
Change your code slightly first to see what you are giving the computer as the -path<\/p>\n
foreach ( $line in $csv )\n{\n $line\n}\n<\/code><\/pre>\n<\/sub><\/sup>Then, see what you get?<\/p>\nI tried this:<\/p>\n
get-childitem | export-csv \".\\test.csv\"\n$a = import-csv \".\\test.csv\"\n\n<\/code><\/pre>\n<\/sub><\/sup>Then, what I found I wanted to do was this, and here’s how I found it:<\/p>\n$a | fl *\n# then I found the fullname field.\nforeach ( $b in $a )\n{\n remove-item -path $b.fullname -recurse -force\n}\n<\/code><\/pre>\n<\/sub><\/sup>I think this will work better for you. Let me know.<\/p>\nHope this helps,<\/p>\n
Jeff<\/p>","upvoteCount":0,"datePublished":"2015-11-13T04:59:08.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/5","author":{"@type":"Person","name":"jeffreycummings","url":"https://community.spiceworks.com/u/jeffreycummings"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hi all,<\/p>\n
I have a list of directories in a CSV file that I am needing to delete. I am using the following script:<\/p>\n
$csv = Import-Csv C:\\temp\\test.csv\nforeach ($line in $csv) {\n Remove-Item -Path $line -Recurse -Force\n}\n<\/code><\/pre>\nHere is an extract of the CSV file:<\/p>\n
C:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Montegrande\\2007 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Montegrande\\2008 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Montegrande\\2009 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Montegrande\\2010 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Z - Clients\\Z - Croftby\\2007 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Z - Clients\\Z - Croftby\\2008 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Z - Clients\\Z - Croftby\\2009 Audit\nC:\\Temp\\General\\Clients\\4 People Pty Ltd\\Admin\\Z - Clients\\Z - Croftby\\2010 Audit\n\n<\/code><\/pre>\nWhen I run the script, I get the following error:<\/p>\n
Remove-Item : Cannot find drive. A drive with the name '@{C' does not exist.\nAt line:3 char:5\n+ Remove-Item -Path $line -Recurse -Force\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : ObjectNotFound: (@{C:String) [Remove-Item], DriveNotFoundException\n + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand\n\n<\/code><\/pre>\nAny advice/help would be great.<\/p>\n
Thanks in advance,<\/p>\n
Dean.<\/p>","upvoteCount":7,"datePublished":"2015-11-12T01:11:46.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/1","author":{"@type":"Person","name":"deanoneill","url":"https://community.spiceworks.com/u/deanoneill"}},{"@type":"Answer","text":"
Is it a true CSV file with a header? If so, please post the header line.<\/p>\n
Or is it just a plain text file with a .csv extension on the file name? If the latter you should be using Get-Content, not Import-CSV.<\/p>","upvoteCount":5,"datePublished":"2015-11-12T01:20:45.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/2","author":{"@type":"Person","name":"bobmccoy","url":"https://community.spiceworks.com/u/bobmccoy"}},{"@type":"Answer","text":"
Additionally, entering a line like<\/p>\n
Write-Output $line \n<\/code><\/pre>\n…in your loop might give you an idea of what is being passed to your remove-item cmdlet.<\/p>","upvoteCount":2,"datePublished":"2015-11-12T01:35:44.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/3","author":{"@type":"Person","name":"Rob-Dunn","url":"https://community.spiceworks.com/u/Rob-Dunn"}},{"@type":"Answer","text":"
Bob and Rob have the right idea. In PowerShell, you have to think of things like objects, not just text. If we break it down like this, we can see what is happening.<\/p>\n
Get-Service | Out-File C:\\Services.csv\n$csv = Import-Csv C:\\Services.csv\n\nforeach ($item in $csv)\n{\nWrite-Output $item\n}\n<\/code><\/pre>\nHere I am outputting all my services to a CSV file, importing it, then writing the Output like Rob suggested. I get a list of services, but it’s not just text, so if I pipe it to Get-Member, I get this:<\/p>\n
$item | GM\n\n TypeName: System.Management.Automation.PSCustomObject\n\nName MemberType Definition \n---- ---------- ---------- \nEquals Method bool Equals(System.Object obj) \nGetHashCode Method int GetHashCode() \nGetType Method type GetType() \nToString Method string ToString() \nH1 NoteProperty string H1=Stopped WwanSvc WWAN AutoConfig \n<\/code><\/pre>\nSo in this case, it created a PSCustomObject for each service (line) in the CSV file. This probably isn’t what you are trying to do. You will probably want to do Get-Content on the file like Bob mentioned, remove what you need, then save that to another file.<\/p>","upvoteCount":2,"datePublished":"2015-11-12T15:34:43.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/4","author":{"@type":"Person","name":"edgrant","url":"https://community.spiceworks.com/u/edgrant"}},{"@type":"Answer","text":"
A couple of notes, you don’t need to loop to see the values in an array, PowerShell will do that for you. So just this:<\/p>\n
$csv\n<\/code><\/pre>\nWould give you everything. PowerShell is all about objects. Every command returns an object, and objects have many properties which is why:<\/p>\n
Remove-Item -Path $line\n<\/code><\/pre>\nDoesn’t work. -Path is expecting a path and you gave it an object which has probably 50 different properties. $Line.FullName works, because that’s the value of one of those properties. But don’t forget about the pipeline, while -Path will only accept a value (UNC path) it is smart enough to look for that property IF the object comes in from the pipeline. So:<\/p>\n
$CSV | Remove-Item -Force\n<\/code><\/pre>\nWould also work. Notice I didn’t specify the path because I know Remove-Item is smart enough to look at the object (and it will only accept a FileSystem object, so CSV better be from a Get-ChildItem!) and find the value it needs. But wait, why didn’t I use -Recurse?<\/p>\n
Well, if $CSV is from Get-ChildItem c:\\testpath*.* then $CSV holds every file and sub-folder in the path, I don’t really need -Recurse since I’m putting every object in $CSV through Remove-Item.<\/p>","upvoteCount":2,"datePublished":"2015-11-13T09:48:06.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/6","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"
Now, to be fair, the above might not fully work either. It’s all a matter of timing. If you try to remove a folder before all the items are gone out of it Remove-Item will throw an error. You should<\/em> be fine but it all depends on how the files come out of Get-ChildItem.<\/p>","upvoteCount":2,"datePublished":"2015-11-13T09:49:30.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/7","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"Have we lost the OP?<\/p>","upvoteCount":0,"datePublished":"2015-11-13T13:18:55.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/8","author":{"@type":"Person","name":"bobmccoy","url":"https://community.spiceworks.com/u/bobmccoy"}},{"@type":"Answer","text":"
This one works fine!<\/p>\n
$path =import-csv C:\\Users\\Documents\\folders.csv\n\nforeach ($line in $path){\nRemove-Item -Path $line.unc -Recurse -force\n\n}\n\n<\/code><\/pre>\nThe csv file is saved in C:\\users\\documents\\folders.csv<\/p>\n
then the first line/ row in the CSV file is named UNC (where we put $line.unc -recurse- force<\/p>\n
Thanks everyone this really simplified my work!<\/p>","upvoteCount":0,"datePublished":"2016-05-03T00:52:27.000Z","url":"https://community.spiceworks.com/t/powershell-remove-item-error/450665/9","author":{"@type":"Person","name":"knychtellcortes","url":"https://community.spiceworks.com/u/knychtellcortes"}}]}}