Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I run a script to process a CSV file once an hour. At the beginning of the script, I need to exit out if the CSV file has not changed. Let's pretend that file timestamps aren't an option. (I'm asking this question for educational value)

I was considering slurping in the entire file and computing the hash on the contents, like so:

$fileData = get-content \path\to\file
$hashCode = $fileData.GetHashCode()

I would then save those contents from run to run, and if the hash is the same on subsequent runs, exit out of my script.

Is there a better way to do this, again, assuming file timestamps are not available?

share|improve this question
up vote 5 down vote accepted

You'll end up using the System.Security.Cryptography namespace to generate that hash. The PowerShell Community Extensions have already done the work for you. There is a Get-Hash cmdlet that returns a HashInfo object you could use for your comparison.

share|improve this answer
That is awesome - thanks. This is what I was looking for. – Larold Sep 30 '11 at 17:41

One thing to note, the Get-HashCode() is not meant to be used as a unique object identifier. It will always be different every time you run it, even in the same session.

Check here and here for more information.

share|improve this answer
Awesome - I had no idea. That is valuable info - thanks! – Larold Sep 30 '11 at 17:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.