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

PowerShell缺失文件检测结果导出CSV并关联原列问题求助

Fixing PowerShell Missing File Detection & CSV Export Issues

First, let's break down what went wrong with your original script, then polish your modified version to get the exact structured CSV output you need—with all missing files and their associated IDs.

Why Your Original Script Failed

1. Only one missing file was exported

  • You were overwriting the $results object every loop iteration ($results = New-Object PSObject...) instead of adding new entries to a collection.
  • Your Export-Csv call didn’t include the -Append flag, so each run overwrote the CSV file instead of adding to it—leaving only the last detected missing file in the final output.

2. Empty ID column

  • The line ID = $CSVexport | Select-Object ID is invalid: $CSVexport is just a string path to your CSV file, not the imported data object. You can’t pull ID values from a file path string; you need to reference the actual rows from your imported CSV.

Fixing Your Modified Script

Your revised script already has the right core logic (checking each CSV entry against the filesystem and linking to the ID), but it’s missing the final step to export a proper structured CSV, and the console output uses PowerShell’s default raw key-value format.

Here’s the polished, working version:

# Define your base directory to avoid repeating hardcoded paths
$baseDirectory = 'C:\Users\Me\Documents'

# Import the original CSV, keeping only the columns we need (ID and File Path)
$fileList = Import-Csv -Path (Join-Path $baseDirectory 'Test.csv') | Select-Object ID, 'File Path'

# Collect all missing files with their corresponding IDs
$missingFiles = $fileList | ForEach-Object {
    # Build the full absolute path to the file
    $fullFilePath = Join-Path $baseDirectory $_.('File Path')
    
    # Check if the file exists (use -PathType Leaf to ensure we're checking for a file, not a folder)
    if (-not (Test-Path -Path $fullFilePath -PathType Leaf)) {
        # Return a custom object with the ID and missing file path
        [PSCustomObject]@{
            ID                  = $_.ID
            'Missing File Path' = $_.('File Path')
        }
    }
}

# Export the results to a new CSV file
$missingFiles | Export-Csv -Path (Join-Path $baseDirectory 'Results.csv') -NoTypeInformation -Encoding UTF8

# Optional: Print the results to the console in a readable table format
$missingFiles | Format-Table -AutoSize

What This Script Does

  • Reduces redundancy: Uses $baseDirectory to build all paths, making the script easier to update later.
  • Preserves ID links: Each missing file entry retains the ID from the original CSV because we iterate directly over the imported CSV rows.
  • Exports a clean CSV: The Export-Csv command creates a structured file with clear columns for ID and Missing File Path.
  • Readable console output: Format-Table gives you a tabular view of results instead of raw key-value pairs.

Example Output

Your final Results.csv will look like this:

ID,Missing File Path
4,Test\1\File4.txt
6,Test\1\File6.txt

And the console output will show:

ID Missing File Path
-- -----------------
4  Test\1\File4.txt
6  Test\1\File6.txt

内容的提问来源于stack exchange,提问作者jl93

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:16:43