You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于CSV列表删除文件夹的PowerShell脚本报错求助

Fixing PowerShell Remove-Item Error with CSV Import

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
}

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:22:46