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
$resultsobject every loop iteration ($results = New-Object PSObject...) instead of adding new entries to a collection. - Your
Export-Csvcall didn’t include the-Appendflag, 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 IDis invalid:$CSVexportis 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
$baseDirectoryto 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-Csvcommand creates a structured file with clear columns forIDandMissing File Path. - Readable console output:
Format-Tablegives 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
相关产品推荐
相关产品推荐

