基于CSV列表删除文件夹的PowerShell脚本报错求助
The core issue here is that Import-CSV returns objects (one for each row in your CSV) instead of plain text strings. When you passed the entire $folder object to the -Path parameter, PowerShell converted it to a string like @{Foldername=G:\Test\a}—which isn't a valid file path. That's why you got the "drive not found" error: it was trying to interpret the start of that messy string as a drive name.
Corrected Code
You just need to access the Foldername property of each object to get the actual folder path:
$folders = Import-Csv G:\Book1.csv foreach ($folder in $folders) { # Grab the actual path from the Foldername property Remove-Item -Path $folder.Foldername -Recurse -Force }
Optional: Add Validation (Recommended)
To avoid unnecessary errors (like if a folder doesn't exist or your CSV has empty rows), add a quick check with Test-Path:
$folders = Import-Csv G:\Book1.csv foreach ($folder in $folders) { $folderPath = $folder.Foldername if (Test-Path -Path $folderPath -PathType Container) { Remove-Item -Path $folderPath -Recurse -Force Write-Host "Successfully deleted: $folderPath" } else { Write-Warning "Skipping - Folder not found: $folderPath" } }
This gives you clear feedback about what's happening, making it easier to debug if anything goes wrong.
内容的提问来源于stack exchange,提问作者weichei69

