Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save moriarty99779/9bbf369aa6bb03434399fcb8ddd33a28 to your computer and use it in GitHub Desktop.
Save moriarty99779/9bbf369aa6bb03434399fcb8ddd33a28 to your computer and use it in GitHub Desktop.
Powershell Get hash of the directory names and attributes
$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()
@moriarty99779
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment