PowerShell: Rename File to Hash

By Xah Lee. Date: . Last updated: .

This script rename all files in a dir into the file content's hash values.

This is useful when you have downloaded thousands of image files on the internet, and want to make sure you don't have duplicates.

Sample file name: 005662EE35110980CEDC1E5B1CD0E23B329C394A65E9134B2EBD0EAE3EC09DC3.jpg

$mydir = "~/Downloads/million_images/";

dir $mydir -file | ForEach-Object {
    $ext = Split-Path -Extension $_;
    $newname = (Get-FileHash $_).hash + $ext;
    Rename-Item $_.fullname $newname }

PowerShell

Practical How-To

PowerShell in Depth

Programing PowerShell

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Read/Write File

Function

Profile and Script

Script Examples