0

I'm a complete beginner to Powershell and scripting, and have been successfully been using Out-GridView to display some properties of the files I have in my directories using the following:

dir D:\Folder1\$type -Recurse | Select Fullname,Directory,LastWriteTime | out-gridview

where I specifiy the file extension with $type = "*.pdf" for instance.

I would also like to start comparing files using hashcodes so I have tried this command:

ls | Get-Filehash

However, I would like to have the hashcodes in the output window as a seperate column with out-gridview. Is this possible? I've tried

dir D:\Folder1\$type -Recurse | Select Fullname,Directory,LastWriteTime,Filehash | out-gridview

and

dir D:\Folder1\$type -Recurse | Select Fullname,Directory,LastWriteTime | Get-Filehash | out-gridview

Of course neither of these work.

Does anyone have a way of generating hashcodes for a specific file extension only?

Many thanks in advance!

4

1 回答 1

0

您可以通过使用计算属性来做到这一点Select-Object

Get-ChildItem -Path 'D:\Folder1\$type'-Recurse |
    Select-Object FullName,Directory,LastWriteTime, @{Label='FileHash'; Expression={(Get-Filehash -Path $_.FullName).Hash}} |
        Out-GridView

您应该会在网格视图中看到一个名为“Filehash”的新列,其中包含文件的 SHA256 哈希值。-Algorithm您可以使用 的参数更改算法(例如,MD5)Get-FileHash

如果您想知道这是在做什么,重要的部分是:

@{...} 表示哈希表。例如一组键值对

label 是定义您的属性(列名)将在网格视图中的键

expression{...}定义计算此属性值的代码片段 ( )

$_ 表示我们正在处理沿管道传递的“当前”对象(在本例中为文件)

于 2019-09-30T12:45:51.020 回答