Add random number prefix to filename: Difference between revisions

From roonics
Jump to navigation Jump to search
(Created page with "This allows you to add a randomly generated number to the prefix of a bunch of files. In the below example we are adding a random prefix to every *.jpg file: <pre> $files =...")
 
No edit summary
 
Line 13: Line 13:


[[Category:PowerShell]]
[[Category:PowerShell]]
‎<comments />

Latest revision as of 10:02, 20 March 2022

This allows you to add a randomly generated number to the prefix of a bunch of files.

In the below example we are adding a random prefix to every *.jpg file:

$files = Get-ChildItem "C:\temp\files" -Filter *.jpg
foreach ($file in $files) {
	$prefix = Get-Random
	$newFileName = "${prefix}_${file}"
	Rename-Item $file $newFileName
}

‎<comments />