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

如何用PowerShell获取电脑全驱动器文件并导出至单个CSV

Export File Info from All Drives to a Single CSV

Got it, let's get this sorted so you can pull file/folder details from every drive on your PC and dump everything into one CSV. First, let's break down what's missing in your current code, then fix it up.

What's Wrong with Your Current Script

Your code only targets the E: drive specifically, and there's a small syntax issue with how you're piping $Path to Select-Object (since $Path is a string, that won't work as intended). To cover all drives, we need to loop through every file system drive on your machine first.

Fixed Script to Cover All Drives

Here's a revised version that scans all drives, collects all the info, and exports it to a single CSV:

# Grab all file system drives (skips non-storage drives like Registry/Certificate)
$allDrives = Get-PSDrive -PSProvider FileSystem

# Create an empty array to store all file/folder data
$allFileData = @()

# Loop through each drive
foreach ($drive in $allDrives) {
    Write-Host "Scanning drive $($drive.Name):\" -ForegroundColor Cyan
    
    # Get all items in the drive, skip access-denied errors to avoid script crashes
    $driveItems = Get-ChildItem -Path "$($drive.Name):\" -Recurse -ErrorAction SilentlyContinue
    
    foreach ($item in $driveItems) {
        # Build a custom object with your desired properties
        $fileEntry = [PSCustomObject]@{
            Name           = $item.Name
            Created        = $item.CreationTime
            filePath       = $item.FullName
            "Size in Bytes" = if ($item.PSIsContainer) { $null } else { $item.Length }
            Modified       = $item.LastWriteTime
            Extension      = if ($item.PSIsContainer) { "Folder" } else { $item.Extension }
        }
        
        # Add the entry to our collection
        $allFileData += $fileEntry
    }
}

# Export everything to a single CSV (adjust the path as needed)
$allFileData | Export-Csv -Path "E:\AllDrives_File_Results.csv" -NoTypeInformation -Encoding UTF8

Key Improvements Explained

  • All Drive Coverage: Get-PSDrive -PSProvider FileSystem fetches every storage drive (local, mapped, etc.) on your system, so we don't have to hardcode drive letters.
  • Error Handling: -ErrorAction SilentlyContinue skips folders you don't have permission to access (like system directories), so the script doesn't stop mid-scan.
  • Cleaner Object Creation: Using [PSCustomObject] makes it easier to define and read the properties we want, avoiding the confusing variable piping from your original code.
  • Single CSV Export: We collect all data first in an array, then export once at the end—this ensures all drive data ends up in the same file instead of overwriting it each time.
  • Logical Size Handling: Folders don't have a byte size, so we set that field to $null for folders (shows as empty in the CSV, which makes more sense than a 0 or blank string).

Quick Notes

  • If you want to exclude certain drives (like a mapped network drive), you can add a filter to $allDrives—for example: $allDrives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Name -notin @("Z", "X") }
  • Depending on the number of files on your system, this might take a while to run—be patient!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:27:23