View all posts filed under 'File System'

Find files with non-inherited security

Sunday, 1. February 2009 19:44

This oneliner shows all folders on which security has been explicitly set.

get-childitem -recurse | where-object {$_.mode -match “d”} | %{$file=$_;get-acl $($_.FullName)} | %{$_.GetAccessRules($True,$False,[Security.Principal.SecurityIdentifier]) | %{write-host “$($file.FullName) has explicit security set”}} 

Download here

Category:File System, PowerShell | Comment (0) | Author: Dennis Damen

Find identical files using PowerShell

Sunday, 1. February 2009 7:50

Does this look familiar? I wanted to sort my collection of photos this weekend. Being the father of a 2 year old girl, I have not hundreds but literally thousands of photos. And with my wife closely monitoring my every move I was making backup after backup. When clicking through my collection, I noticed that I had quite a few identical pictures. Probably caused by previous “backup and sort” actions like this one.

So, I needed to find a way to find identical files and have fun at the same time. Instead of just installing some tool I decided to turn to PowerShell. Using a hashtable and an MD5 hashing routine I was able to quickly find and cleanup my photos collection.

For those of you not familiar with a hashtable. A hashtable is a table that consists of two columns called ‘key’ and ‘value’. Basically you can store anything in those columns like so: $hashTable.Add(“SomeKey”, “SomeValue”). The ‘key’, however, needs to be unique. So storing “SomeKey”, “SomeValue” twice would generate an exception. I will be using this behavior in my script.

So, how does this script work? This script will find all files and it will try to store the MD5 hash of those files as the ‘key’ value in the hashtable. When a file is encountered with the same MD5 hash as a previous file, the hashtable will generate an exception. This exception is caught by the ‘trap’ routine which will display the current file as well as the name of file which was stored in the hashtable earlier.

param ( $path=”.\”, $extension=”*.*” )

$hashtable = new-object system.collections.hashtable
$cryptoServiceProvider = [System.Security.Cryptography.MD5CryptoServiceProvider]
$hashAlgorithm = new-object $cryptoServiceProvider

get-childitem -path $path $extension -recurse |where-object {$_.mode -notmatch “d”}| %{ $file = $_ ; trap { write-host “$($file.Fullname) is identical $($hashtable[`"$([string] $hashAlgorithm.ComputeHash($file.Openread()))`”])” -foregroundcolor yellow;continue; } ; [string] $hashAlgorithm.ComputeHash($file.Openread()) | %{$hashTable.Add($_,$file.FullName)} ;}

Download here

Category:File System, PowerShell | Comment (0) | Author: Dennis Damen