Created
May 6, 2024 15:00
-
-
Save moriarty99779/9bbf369aa6bb03434399fcb8ddd33a28 to your computer and use it in GitHub Desktop.
Powershell Get hash of the directory names and attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$pwd = "C:\Users\someusername" | |
$dirinfo = Get-ChildItem($pwd) -recurse | |
$d = $dirinfo.ToString() | |
$stringAsStream = [System.IO.MemoryStream]::new() | |
$writer = [System.IO.StreamWriter]::new($stringAsStream) | |
$writer.write($d) | |
$writer.Flush() | |
$stringAsStream.Position = 0 | |
$output = Get-FileHash -InputStream $stringAsStream | Select-Object Hash | |
$output.hash.ToString() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist may not be clear at first, so let me explain in a bit more detail. When you run the snippet, a string is created that contains the hashed value of the output of the parent directory with Get-ChildItem (similar to dir or ls) function as well as the output of any subdirectories. This hash is put into a string. The idea is that one would store this value someplace (such as a database, or a line in a file, etc.) and then it could be used to compare directories from one time period versus another. If the hash values are different, then one of the files or its corresponding attributes has changed, with will cause a different hash value to be generated.