Unable to get output from get-filehash - powershell

I am looking for a reliable command-line method of getting SHA256 hashes for files in Windows. My understanding is that the way to do this is via Microsoft's Get-FileHash cmdlet under PowerShell. I have seen several web sites with examples and reviewed Microsoft's own documentation. It appears that the following syntax should work on Windows Server 2012:
Get-FileHash myfile.txt -Algorithm SHA256
The command runs without error, but there is no output. If I send the output to a file, the file is created with no content. I have also seen examples which pipe the output to Format-List; I tried that, but still nothing. I have also tried running the command with invalid arguments, and again nothing.
I am open to using a different program, but due to business requirements, it would need to be a supported download.

I'm using PowerShell 4.0 and I just encountered the same problem of null output from Get-FileHash. The cause of my problem is different than the OP but I have found a solution to my problem and I figured I would post my findings for anyone who came to this page trying to solve the problem of null output (or seemingly incorrect output) from Get-FileHash.
The problem only happens (for me) when the path to the target file contains brackets [ ] and those brackets contain either zero characters or 2 or more characters.
EDIT: I now understand WHY this happens. The string is interpreted as Regular Expression (RegEx) so the square brackets [ ] take on their special RegEx meaning. The -LiteralPath tells PowerShell to interpret the string as a simple match (no RegEx).
Consider the following paths which refer to 4 existing text files (hypothetically):
C:\Test\My Text.txt
C:\Test\My [Text].txt
C:\Test\My [Te]xt.txt
C:\Test\My Text[].txt
The following command produces normal output:
Get-FileHash "C:\Test\My Text.txt"
but there will be null output if using the following commands:
Get-FileHash "C:\Test\My [Text].txt"
Get-FileHash "C:\Test\My [Te]xt.txt"
Get-FileHash "C:\Test\My Text[].txt"
This can be solved by using the -LiteralPath switch. For example:
Get-FileHash -LiteralPath "C:\Test\My [Text].txt"
Variables are expanded normally when using the -LiteralPath switch. For example:
(Get-ChildItem C:\Test).FullName | ForEach {
Get-FileHash -LiteralPath $_
}
If there is exactly 1 character between the brackets, the brackets will be ignored when using Get-FileHash.
Consider the following paths which refer to 3 existing text files (hypothetically), each with unique hash values:
C:\Test\My Text.txt
C:\Test\My Tex[t].txt
C:\Test\My[ ]Text.txt
Get-FileHash interprets all three of the following commands in exactly the same way ( the path is interpreted as C:\Test\My Text.txt ) and therefore each command has the exact same output despite each file having it's own unique hash value:
Get-FileHash "C:\Test\My Text.txt"
Get-FileHash "C:\Test\My Tex[t].txt"
Get-FileHash "C:\Test\My[ ]Text.txt"
P.S. I'm a very new programmer, please forgive me for any poor usage of terminology.

Get-FileHash, requires Windows PowerShell 4.0
Based on your comments you are at version 3, which is default on Win 2012 (non R2) Here how to check you PS version
You can update PS on Win 2012 (non R2) to version 4.0 or use Win 2012 R2
If you just run Get-FileHash on a PS version 3 system you should get
PS C:\> Get-FileHash
Get-FileHash : The term 'Get-FileHash' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-FileHash
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-FileHash:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
On lower PS version you can use this function
Function Get-FileHashTSO([String] $FileName,$HashName = "SHA1")
{
$FileStream = New-Object System.IO.FileStream($FileName,[System.IO.FileMode]::Open)
$StringBuilder = New-Object System.Text.StringBuilder
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash($FileStream)|%{[Void]$StringBuilder.Append($_.ToString("x2"))}
$FileStream.Close()
$StringBuilder.ToString()
}
store it as .ps (e.g. Get-FileHashTSO.ps1) file and call it like this
powershell -command "& { . C:\myScripts\Get-FileHashTSO.ps1 ; Get-FileHashTSO "C:\someLocation\someFile.iso" "SHA1" }"

I calculated the hash of all files on a drive and exported them to a .csv by using this:
Get-ChildItem C: -Recurse |
Get-FileHash |
Export-Csv -Path C:\Users\yourname\Documents\Output\hashes.csv -NoTypeInformation
Import-Csv -Path C:\Users\yourname\Documents\Output\hashes.csv
Why it works (I think):
Get-ChildItem <--gets everything under your path, in this case C:, and -Recurse gets all the files within folders. You can add limitations if needed.
Get-FileHash <--after you've said get these files, you're saying calculate the hashes
Export-Csv <--says, we're sending your hashes out as comma separated values file, which is crazy helpful, and -Path says put it HERE, -NoTypeInformation just removes the #TYPE row from the top of the .csv file, and versions of PowerShell before 6 need this.
Import-Csv <--says, bring that data into the file at this -Path
Be sure to have the .csv file created in that location before you run the script. The script won't create the container for you. There's no need to clear the data from the .csv file between runs, it clears itself.
I'm not really a programmer, hence the annoyingly lengthy explanation. Hope it helps others. Wouldn't have figured it out without Stack Overflow forums! Thanks everyone!!

Related

Using PowerShell to pass all files of a folder as arguments to a command line program

I'm trying to create a file listing of a folder for a secure file transfer tool. This is what I do:
Get-ChildItem c:\files | % {$_.FullName} > c:\temp\list1.csv
$csv = Import-Csv C:\TEMP\list1.csv -Header Path
The output holds every file in a new line, but I need it in one line.
Required output
"C:\files\Alpha" "C:\files\Beta" "C:\files\Gamma" "C:\files\Delta"
Actual output
C:\files\Alpha
C:\files\Beta
C:\files\Gamma
C:\files\Delta
The csv file is just what came to my mind first. A variable containing the files formatted like mentioned above would be sufficient. Do you have an idea?
Edit: Thank you #Matthias R. Jessen and #WaitingForGuacamole, you gave me exactly what I wanted.
(Get-ChildItem C:\scripts -File).ForEach({'"{0}"' -f $_.FullName.Replace('"','\"')}) -join " "
However, somehow my tool (written in java) is interpreting the output as one file instead of multiple files in a line.
Below the error message:
Java : Error: The file 'C:\files\Alpha C:\files\Beta C:\files\Delta C:\files\Gamma' was not found and is excluded from the transfer.
I know, that I have to handover the paths differently when using a properties file instead of entering the command manually in PowerShell.
Is there a way on letting the output look like:
"C:\\files\Alpha" "C:\\files\Beta" "C:\\files\Gamma" "C:\\files\Delta"
To pass the file paths of all children of a specific folder to a command line program as separate arguments, just pass the results of
(Get-ChildItem -File).FullName
to the program. Example:
$files = (Get-ChildItem C:\MyFolder -File).FullName
# Expected: myprogram.exe -arg1 -arg2 C:\MyFolder\file1.txt C:\MyFolder\file2.txt ...
myprogram.exe -arg1 -arg2 $files

MD5-Checksum hashing with powershell for a whole directory

I'm trying to generate an MD5-Checksum with powershell for a whole directory.
On Linux there is a 1-liner that works just great, like this one:
$ tar -cf - somedir | md5sum
I learned that "tar" is now part of Windows10 and that it can be adressed in the PowerShell.
So I tried this:
tar -cf - C:\data | Get-FileHash -Algorithm MD5
What I get from PowerShell is this:
tar.exe: Removing leading drive letter from member names
Get-FileHash : the input object cannot be bound to any parameters of the command because the command does not accept pipeline input or the input and its properties do not match any of the parameters that accept pipeline Input
My Shell is set to german, so I ran the german error text through a Translation machine (https://www.translator.eu/#).
I wondered why I get this particular error message, because Get-FileHash IS able to process pipelined Input, e.g.:
ls | Get-FileHash -Algorithm MD5
This command just works like a charm, but it gives me checksums for each and every file.
What I want is 1 checksum for a complete given directory.
So, I probably messed up something… - any ideas?
EDIT: Here's an alternate method that is consistent even if all the files are moved/copied to another location. This one uses the hashes of all files to create a "master hash". It takes longer to run for obvious reasons but will be more reliable.
$HashString = (Get-ChildItem C:\Temp -Recurse | Get-FileHash -Algorithm MD5).Hash | Out-String
Get-FileHash -InputStream ([IO.MemoryStream]::new([char[]]$HashString))
Original, faster but less robust, method:
$HashString = Get-ChildItem C:\script\test\TestFolders -Recurse | Out-String
Get-FileHash -InputStream ([IO.MemoryStream]::new([char[]]$HashString))
could be condensed into one line if wanted, although it starts getting harder to read:
Get-FileHash -InputStream ([IO.MemoryStream]::new([char[]]"$(Get-ChildItem C:\script\test\TestFolders -Recurse|Out-String)"))
Basically it creates a memory stream with the information from Get-ChildItem and passes that to Get-FileHash.
Not sure if this is a great way of doing it, but it's one way :-)

Prevent PowerShell Select-String from parsing colon as a drive letter

I have a profile that gets executed every time I open my PowerShell window. Since I do some SSH'ing, I need to ensure that my gets added right after I start up PowerShell. But I want to avoid asking my passphrase for the SSH as long as it's already added to the ssh-agent. The solution to this problem is described here, but for the Linux shell.
I have converted the solution described there to a PowerShell equivalent, which is as follows:
$ssh_add = "$env:ProgramFiles/Git/usr/bin/ssh-add.exe"
$ssh_keygen = "$env:ProgramFiles/Git/usr/bin/ssh-keygen.exe"
$my_key_path = "$env:USERPROFILE/.ssh/id_rsa"
$my_ssh_key = & $ssh_keygen -lf $my_key_path
$ssh_keys = & $ssh_add -l
if (!(Select-String -Pattern $my_ssh_key -Path $ssh_keys -SimpleMatch -Quiet))
{
& $ssh_add -t 5h $my_key_path
}
The SSH keys happen to contain colons, which PowerShell seems to think are drive letters. This results in the following error message:
Select-String : Cannot find drive. A drive with the name '4096 SHA256' does not exist.
The SSH keys in the following form:
$my_ssh_key = 4096 SHA256:somelongSSHkey some.email#stackoverflow.com (RSA)
$ssh_keys = 4096 SHA256:anotherlongSSHkey /c/Users/MyUser/.ssh/id_rsa (RSA)
How can I prevent the colon from being parsed as a drive letter seperator?
-Path is supposed to be a path. The value you are providing to it will, naturally, be interpreted as a path. You could escape the colon, but that doesn't solve your problem of telling Select-String to treat something as a path when it isn't a path.
You want the -InputObject (can be shortened, as all PS arguments can) parameter where you're using -Path right now. That will match the literal string, rather than treating it as a file to search. You can also pipe the contents of $ssh_keys to Select-String instead, and not specify any path.
if (!(Select-String -Input $ssh_keys -Pattern $my_ssh_key -SimpleMatch -Quiet))
...
See https://technet.microsoft.com/library/hh849903.aspx for full documentation.

Trying to copy a group of files contained in a text file

I'm trying to copy a list of files from a txt file and as a newbie, I'm having a hard time.
Here is a bit of the text file. The real file has no extra lines, but I had to do that to :
"D:\Shared\Customer Care\Customer Care Common\Customers Contracted\Customers Contracted\Fred 44705"
"D:\Shared\Customer Care\Customer Care Common\Customers Contracted\Customers Contracted\Johnson 47227"
"D:\Shared\Customer Care\Customer Care Common\Customers Contracted\Customers Contracted\Daniel 35434"
"D:\Shared\Customer Care\Customer Care Common\Customers Contracted\Customers Contracted\Frank, John 48273"
I've tried enclosing the filename string in double-quotes as well.
Here's the simple script I'm trying to use:
Get-Content c:\users\scripts\files-to-fix.txt | Foreach-Object {copy-item $_ d:\junk}
The error I'm getting is:
Copy-Item : Cannot find drive. A drive with the name ''D' does not
exist. At C:\users\mhyman\scripts\copyfiles.ps1:2 char:81
+ Get-Content c:\users\mhyman\scripts\files-to-fix.txt |
Foreach-Object {copy-item <<<< $_ d:\junk}
+ CategoryInfo : ObjectNotFound: ('D:String) [Copy-Item],
DriveNotFoundException
+ FullyQualifiedErrorId :
DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
I know this is simple, but I would really appreciate some help.
I think it is the surrounding quotes that are causing the problem ( as indicated by the error saying that a drive of name "D is not found. Try this:
get-content c:\users\scripts\files-to-fix.txt | %{ copy-item $_.trim('"') d:\junk}
Of course, if you can control the txt file, enter the list without the quotes.
By your tags and drive letters and backslashes it is clearly a Windows environment your working in and although I'm not a PowerShell scripter, I'm a better than most batch scipter and use a For / If conditioanla statement sicne it is shorter and you feed it your file instead of parsing out the file into reduudc commands on a line, so in your example:
for /F %%t in (the text file.txt) do copy /q %%t d:\junk
And then you go home and never worry about until the next morning
Does powershell have a runas ornative mode that can parse older, more proven and stable DOS commands ?

Test-Path behavior in Powershell

I'm trying to write a script in powershell to batch convert video files.
The way I intend to use it is to go to some folder full of video files and run it. It uses a conversion program that can be run in "command-line mode" (named handbrake) and saves the converted files with "-android" appended to them before the file extension. For example, if I have a file named video1.avi in the folder, after running the script the folder has 2 files: video1.avi and video1-android.avi
The reason I want to do this this way is so that I can check if, for each video file, there is a converted version of it (with -android appended to the name). And if so, skip the conversion for that file.
And this is where I'm having touble. The problem is the Test-Path's behavior (the cmdlet I'm using to test if a file exists).
What happens is, if the video file has an "unusual" name (for example in my case it's video[long].avi) Test-Path always returns False if you try to check if that file exists.
An easy way for you to test this is for example to do this:
Go to an empty folder,
run notepad to create a file with "[" in its name:
&notepad test[x].txt
Save the file
then do this:
Get-ChildItem | ForEach-Object {Test-Path $_.FullName}
It does not return true! It should right? Well it doesn't if the file has "[" in its name (I didn't check for any other special characters)
I've realized that if you escape the "[" and "]" it works
Test-Path 'test`[x`].txt'
returns true.
How can I go around this issue? I want to be able to: given a BaseName of a file, append it "-android.avi" and check if a file with that name exists.
Thanks,
Rui
Many PowerShell cmdlets have Path parameters that support wildcarding. As you have observed, in PowerShell not only is * a wildcard but [ and ] are also considered wildcard characters. You can read more about this in the help topic about_Wildcards.
For your issue, if you don't need wildcarding then I would recommend using the -LiteralPath parameter. This parameter doesn't support wildcarding and accepts [ and ] as literal path characters e.g.:
Get-ChildItem | ForEach {Test-Path -LiteralPath `
"$([io.path]::ChangeExtension($_.FullName,'avi'))"}
FYI, the reason piping the output of Get-ChildItem directly into Test-Path works is because the LiteralPath parameter has an alias "PSPath" that maps to the PSPath property on the FileInfo object output by Get-ChildItem. That property gets bound to the LiteralPath (er PSPath) parameter "by property name".
dir | % {test-path "$($_.Name)-android.avi"}