PowerShell : Finding Duplicate Files, Part 1 : The Basic Script

We have a lot of photos, music and files.  Normally we copy the files up to some network storage (so it’s backed up) and later on we come back and name and sort everything.  But then, some chaos happens;  maybe we get distracted so end up copying the files twice.  Or maybe we’ve been away and I’ve backed them up to another device and then copied two sets of files later.  Or a set of files were copied to another computer and some of them (but not all) were modified before copying them back.

The upshot is that there’s precious NAS storage being wasted.  But how to find where the duplicate files are?  Sounds like an excuse reason to put on my scripting hat!

Subsequently to writing this I did some more work on the script.  So now there are two more parts;  this post covers the basic script while part 2 details putting code in to allow resuming the script after a restart (or crash) and finally part 3 does the same thing but using PowerShell workflows.

Continue reading “PowerShell : Finding Duplicate Files, Part 1 : The Basic Script”

PowerShell : Record Process Time Script (Updated)

I’ve had the script I wrote to keep a record of certain processes run time running for  a while (I’m using it to keep a log of the games I play and when).  One thing I noticed though is that there are plenty of processes matching the path I’m interested in (anything with “games” in the file path) that I’m not interested in.  Launchers, background tasks, updaters or services for example.

What I need to do is set a list of exclusions and then make sure any process that matches the other criteria isn’t listed in the exclusions.  The changes are below;

Continue reading “PowerShell : Record Process Time Script (Updated)”

PowerShell : Recording Process Run Time (Or, How Long Have I Played That Game For?)

I did a post about all the games I played in 2014.  The only source of data I had was the total play-time from Steam, but I really wanted a way of getting more detailed information (like when I played them and how long for).

I thought about it and I what I wanted was script that would record the time processes that were running on my system and write them to a file.  The script would stay running and update the file at periodic intervals to record how long the processes  had been running for.

Continue reading “PowerShell : Recording Process Run Time (Or, How Long Have I Played That Game For?)”

PowerShell: Synchronizing a Folder (and Sub-Folders) Part 1

There are plenty of commercially available systems for synchronizing the contents of two folders (I’ve used Allway Sync a bit and it works well).  But what if you just want to quickly sync a folder (and sub-folders) to another location using PowerShell?

Ideally I’d want it to take a source folder and synchronize it to a target folder with any missing content being recreated.  If the target folder is already populated, I’d want to update any newer versions of the files and remove anything that has been deleted from the source.

The script (and a breakdown on how it works) follows after the line.

Update : I’ve revisited this script a few times with new additions and modifications.The latest full version of the script is here.  That post also includes links covering the other revisions to the script.

Continue reading “PowerShell: Synchronizing a Folder (and Sub-Folders) Part 1”

PowerShell: Synchronizing a Folder (and Sub-Folders)

This is the latest version of my PowerShell folder synchronisation script.

I’ve revisited this script a few times with new additions and modifications.  If you want explanations about how the script was developed over time or how parts of the script work, have a look at the following posts;

Part 1 covers the basic script to sync two folders.

Part 2 adds the ability to specify exceptions that the sync process will skip.

In part 3 I added an XML configuration file so more complicated processes can be run.

I customised and validated parameter input (parameter sets) and added some proper error checking in part 4.

The script was updated to deal with non-standard paths and to output objects reporting on the changes it made in part 5.

In part 6 I add more use of literal paths (to prevent path errors) and generate statistics.

I add a filter option, turn strict-mode on and optimise the statistics generation in part 7 (plus fix a bug!).

In part 8 I’ve added some validity checks on the XML configuration file.

WhatIf functionality was added in Part 9 plus some corrections to make sure Filter and Exceptions worked correctly.

In Part 10 I added text file logging.

In Part 11 I fixed an odd bug where $Nulls were being added to arrays and added a -PassThru switch.

Part 12 covers some fixes for behaviour and adding the ability to support  multiple target folders (so the source folders can be synced to multiple locations).

Part 13 added more control over what gets logged and how, added a timestamp to the log filename, creating a new log on each run, code cleanup and some bug- fixes.

A zipped version of the script is here;

Continue reading “PowerShell: Synchronizing a Folder (and Sub-Folders)”

PowerShell : Comparing Contents of Two Folders

Someone at work asked me if I knew a quick way in PowerShell to compare the contents of two folders. I asked a few questions and it turned out he was trying to rationalize all the music folders he had scattered round on his families computers. He wanted a way to look at all the files in a folder and check if an identical file (by identical he meant the same size) was present in a second folder.

I quickly thrashed out a line which he later said did the trick;

gci c:\source | %{$Found=$false;foreach($Item2 in $(
gci d:\target)){if($_.Name -eq $Item2.Name -and
$_.Length -eq $Item2.Length){$Found=$true}};
if (-not $Found){"$($_.Name) not matched."}}

Explanation about how it works below the line.
Continue reading “PowerShell : Comparing Contents of Two Folders”