0
votes

i have to delete folders based on a excel list, so i have tried to import the execl with import csv but it dosent work. The import doesnt fill the variable in the correct format. This is the code i tried to use:

$folders = import-csv G:\Book1.csv foreach ($folder in $folders) {Remove-Item -Path $folder -Recurse -Force}

The error is this: Remove-Item : Cannot find drive. A drive with the name '@{Foldername=G' does not exist. At line:4 char:5 + Remove-Item -Path $folder -Recurse -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (@{Foldername=G:String) [Remove-Item], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot find drive. A drive with the name '@{Foldername=G' does not exist. At line:4 char:5 + Remove-Item -Path $folder -Recurse -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (@{Foldername=G:String) [Remove-Item], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot find drive. A drive with the name '@{Foldername=G' does not exist. At line:4 char:5 + Remove-Item -Path $folder -Recurse -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (@{Foldername=G:String) [Remove-Item], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

In the CSV in the 1.Line heading stands foldername in line 2-4 are the name of the folders (a, b, c). This is the CSV:

Foldername
G:\Test\a
G:\Test\b
G:\Test\c

The directory is G:\Test\a , b, c

1
Please edit your question and show the first 3 or 4 lines of the G:\Book1.csv as Formatted text. From your description it does not look like a CSV file at all..Theo
I got the XLS File and save it as CSV, can i do anything other? Thank you for your helpweichei69

1 Answers

0
votes

I have found a Solution, the CSV file was a Problem and the import so i had to modify the CSV File: "Foldername"
"G:\Test\a"
"G:\Test\b"
"G:\Test\c"

the powershell code is this now:

$folders = import-csv .csv | ForEach-Object {

    write-host $($_.Foldername)
    Remove-Item -Path $($_.Foldername) -Recurse -Force     
 }