Windows PowerShell – Get-FileHash and Long File Paths

checksumpathpowershellwindowswindows 10

OS is Windows 10.

I'm using PowerShell's "Get-FileHash" command to generate a file's MD5 checksum directly within its folder, like so:

PS Z:\Test> Get-FileHash 'test.txt' -algorithm MD5                                                                      
Algorithm       Hash                                                                   Path
---------       ----                                                                   ----
MD5             D41D8CD98F00B204E9800998ECF8427E                                       Z:\Test\test.txt

However, when long file paths are involved, PowerShell seems unable to find the file trough Get-FileHash.

PS Z:\Test loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong> Get-FileHash 'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong.txt' -algorithm MD5                                                                                                                Resolve-Path : Cannot find path 'Z:\Test looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooong\looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooong.txt' because it does not exist.
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Utility\Microsoft.PowerShell.Utility.psm1:11
0 char:36
+                 $pathsToProcess += Resolve-Path $Path | Foreach-Objec ...
+                                    ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Z:\Test loooooo...ooooooooong.txt:String) [Resolve-Path], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand

On the other hand, it's perfectly able to find the file using "Get-ChildItem":

PS Z:\Test loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong> Get-ChildItem


    Directory: Z:\Test looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
    ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
    oooooooooooooooooooooooooong


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2024-02-26     12:55              0 loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
                                                  ooooooooooong.txt

How do I generate MD5:s in these kind of cases? I don't have administrative privileges.

Best Answer

You can specify an extended-length path by prepending \\?\ to it and using the -LiteralPath parameter, like:

# local path
Get-FileHash -LiteralPath '\\?\C:\long\path\to\file.txt'

# network share
Get-FileHash -LiteralPath '\\?\UNC\server\share\folder\file.txt'

This convinces most programs to accept long path names, and does not require admin permissions.

See more information in the answer here: https://stackoverflow.com/a/21194605/7411885

Related Question