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

如何在PowerShell文件共享清单CSV中添加ID与父ID字段

Adding Unique ID and Parent ID to Your PowerShell File Inventory CSV

Hey there! Let's get your script updated to include unique IDs and corresponding parent IDs for each entry in your CSV. Here's a straightforward approach to make this work:

Core Idea

First, we'll collect all your files and folders upfront, assign each a unique numeric ID, and build a lookup table that maps each item's full path to its ID. Then, when processing each item, we can use this lookup table to quickly find the ID of its parent folder.

Modified Script

#Set-ExecutionPolicy Unrestricted
$SourcePath = "G:\My Drive"
$DestinationCSVPath = "e:\G Drive Inventory 20180611.csv"

# Update column order to include our new ID fields
$CSVColumnOrder = 'ID', 'ParentID', 'Path', 'IsDIR', 'Directory', 'FileCount', 'Parent', 'Name', 'CreationTime', 'LastAccessTime', 'LastWriteTime', 'Extension', 'BaseName', 'B'

# Get all files and folders first (we need this to build our ID lookup map)
$SourcePathItems = Get-ChildItem $SourcePath -Recurse

# Create a hashtable to map each item's full path to its unique ID
$PathToIdMap = @{}
$currentId = 1

# First pass: assign IDs and populate the lookup map
foreach ($item in $SourcePathItems) {
    $PathToIdMap[$item.FullName] = $currentId
    $currentId++
}

# Second pass: build output objects with ID and ParentID fields
$HashOutput = ForEach ($file in $SourcePathItems) {
    # Determine ParentID: root items have no parent, so set to null
    $parentId = $null
    if ($file.DirectoryName) {
        # For files/subfolders, fetch parent directory's ID from our lookup map
        $parentId = $PathToIdMap[$file.DirectoryName]
    }

    # Create the custom object with our new fields
    Write-Output (New-Object -TypeName PSCustomObject -Property @{
        ID = $PathToIdMap[$file.FullName]
        ParentID = $parentId
        Path = $file.FullName
        IsDIR = $file.PSIsContainer
        Directory = $File.DirectoryName
        FileCount = (Get-ChildItem $File.FullName -Recurse -ErrorAction SilentlyContinue).Count
        Parent = $file.Parent
        Name = $File.Name
        CreationTime = $File.CreationTime
        LastAccessTime = $File.LastAccessTime
        LastWriteTime = $File.LastWriteTime
        Extension = $File.Extension
        BaseName = $File.BaseName
        B = $File.Length
        #Root = $file.Root
        #IsReadOnly = $file.IsReadOnly
        #Attributes = $file.Attributes
        #Owner = $acl.owner
        #AccessToString = $acl.accesstostring
        #Group = $acl.group
        #MD5 = Get-FileHash $file.FullName -Algorithm MD5 | Select-Object -ExpandProperty Hash
        #SHA1 = Get-FileHash $file.FullName -Algorithm SHA1 | Select-Object -ExpandProperty Hash
    }) | Select-Object $CSVColumnOrder
}

# Export the final output to CSV
$HashOutput | Export-Csv -NoTypeInformation -Path $DestinationCSVPath

Key Changes Explained

  • PathToIdMap Hashtable: This stores every item's full path as a key, paired with an incrementing unique ID as the value. It lets us quickly look up any parent folder's ID later.
  • ID Field: Pulls directly from our lookup map to guarantee each entry gets a unique identifier.
  • ParentID Field: Checks if the item has a parent directory, then uses the lookup map to fetch that parent's ID. The root folder will have a ParentID of null since it has no parent.
  • Updated CSVColumnOrder: Added ID and ParentID to the front of the column list so they're easy to spot in your CSV.
  • Error Handling for FileCount: Added -ErrorAction SilentlyContinue to avoid crashes if some folders are inaccessible (you can adjust this if you want to log errors instead).

This setup will give you a clean CSV where each row has its own unique ID, and you can trace the parent-child folder structure using the ParentID field.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:47:09