如何在PowerShell中创建md5sum别名?配置报错求助
md5sum Alias in PowerShell The error you're seeing happens because Set-Alias expects a command name (like a function or cmdlet) as its target, not the output of running a command. When you try to set the alias to (file_hash -algorithm 'MD5' -file $args), PowerShell immediately runs that function call (with $args being empty at the time), which returns null—hence the "Cannot bind argument to parameter 'Value' because it is null" error.
Here are two straightforward ways to get your md5sum alias working:
Solution 1: Create a dedicated md5sum function
This is the simplest approach—just define a function that directly computes the MD5 hash for a given file, no alias needed (since the function itself is named md5sum):
function md5sum { param ( [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] $file ) # Handle pipeline input if provided process { if ($file) { (Get-FileHash -Path $file -Algorithm MD5).Hash } } }
Now you can use it exactly like you want:
md5sum path/to/your/file.txt
It also supports pipeline input, just like your original function:
Get-ChildItem *.txt | md5sum
Solution 2: Keep your generic file_hash function and add a wrapper
If you want to retain your multi-algorithm file_hash function, create a wrapper function that calls it with MD5, then alias that wrapper to md5sum:
# Your existing generic function (updated with process block for pipeline support) function file_hash { param ( [Parameter(Mandatory = $true, Position = 0)] $algorithm, [Parameter(Mandatory=$false, ValueFromPipeline=$true, Position = 1)] $file ) process { if ($file) { (Get-FileHash -Path $file -Algorithm $algorithm).Hash } } } # Wrapper function for MD5 function md5sum_wrapper { param ( [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] $file ) process { file_hash -algorithm MD5 -file $file } } # Alias the wrapper to md5sum Set-Alias md5sum md5sum_wrapper
This way you can still use file_hash with other algorithms (like file_hash SHA256 myfile.txt) while having the convenient md5sum alias for MD5 checks.
Either solution will let you run md5sum file and get the MD5 hash as expected.
内容的提问来源于stack exchange,提问作者FloH96




