简体   繁体   中英

Getting all files in a directory with PowerShell Get-ChildItem

I have been using the following command to get the MD5 hashes for all files in a directory (and all its subdirectories):

Get-FileHash -Algorithm MD5 -LiteralPath (Get-ChildItem "*.*" -Recurse)

However, I realised that a few of the subdirectories have files with no file extension.

What is the difference between the following two commands and is either a good way to get all files in a directory (including files without a file extension)? Their outputs appear to be the same for my test directory yet only the first one works as an input for the Get-FileHash cmdlet.

Get-ChildItem "*" -Recurse

Get-ChildItem -Recurse | where {!$_.PsIsContainer}

Edit: Thank you Mathias, these both appear to work with Get-FileHash (including files with no file extension and also files with square brackets in the filename):

Get-FileHash -Algorithm MD5 -LiteralPath (Get-ChildItem "*" -Recurse)

Get-ChildItem -Recurse | where {!$_.PsIsContainer} | Get-FileHash -Algorithm MD5

It helps to post the error messages. This would work, if you really wanted to do it this way. This is part of an annoying problem with PS 5, where the string version of what get-childitem returns isn't the full path. Strange that *.* returns the full path. One workaround is to pipe it to get-item after that.

get-filehash -Algorithm MD5 -LiteralPath (Get-ChildItem -Recurse | get-item | 
where {!$_.PsIsContainer})

Another way. Get-ChildItem has a -File option now. And get the fullname.

get-filehash -Algorithm MD5 -LiteralPath (Get-ChildItem -Recurse -File).fullname

Simple demo of the problem:

get-childitem * -recurse | foreach-object { "$_" }

foo2
hi.doc
hi2.doc


get-childitem *.* -recurse | foreach-object { "$_" }

C:\Users\js\foo\foo2\hi2.doc
C:\Users\js\foo\hi.doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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