简体   繁体   English

如何将校验和添加到 Get-ChildItem -Recurse?

[英]How to add checksum to Get-ChildItem -Recurse?

I've been using a very simple Get-ChildItem -Recurse command to generate a CSV inventory of a file directory for appraising said files for retention/deletion.我一直在使用一个非常简单的 Get-ChildItem -Recurse 命令来生成一个文件目录的 CSV 清单,用于评估所述文件的保留/删除。 I'm very new to PowerShell and I'm trying to keep the code as simple as possible, here is what I've been successfully using:我对 PowerShell 非常陌生,我正在努力使代码尽可能简单,这是我成功使用的内容:

Get-ChildItem -Recurse | Select-Object FullName, Name, Extension, Length, CreationTime, LastAccessTime, LastWriteTime | Export-Csv "C:\file\path\file_list.csv"

But I would like to add a checksum to the Select-Object, I tried the following code but it created a CSV with only the checksum and no other file data.但是我想向 Select-Object 添加校验和,我尝试了以下代码,但它创建了一个只有校验和而没有其他文件数据的 CSV。 The checksum should be a column in the CSV next to the other Select-Object parameters.校验和应该是 CSV 中其他 Select-Object 参数旁边的一列。

Get-ChildItem -Recurse | Get-FileHash -Algorithm MD5 | Select-Object FullName, Name, Extension, Length, CreationTime, LastAccessTime, LastWriteTime, Hash | Export-Csv "C:\file\path\file_list.csv"

Any help is greatly appreciated, I'm VERY new to PowerShell, thanks!非常感谢任何帮助,我对 PowerShell 非常陌生,谢谢!

Since you want to add it as an additional property, you can use a calculated property like this.由于您想将其添加为附加属性,因此您可以使用这样的计算属性

Get-ChildItem -Recurse |
    Select-Object FullName, Name, Extension, Length, CreationTime, LastAccessTime, LastWriteTime, @{n="Hash";e={(Get-FileHash $_.FullName -Algorithm MD5).hash}} |
        Export-Csv "C:\file\path\file_list.csv"

To make it easier to read and maintain, you could use a variable to hold the desired properties like this.为了更易于阅读和维护,您可以使用一个变量来保存这样的所需属性。

$props = "FullName",
         "Name",
         "Extension",
         "Length",
         "CreationTime",
         "LastAccessTime",
         "LastWriteTime",
         @{n="Hash";e={(Get-FileHash $_.FullName -Algorithm MD5).hash}}

Get-ChildItem -Recurse | Select-Object $props | Export-Csv "C:\file\path\file_list.csv"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM