Copy Directory Files and Create Random Names In PowerShell

I had a need to take an existing directory with a specific set of files and create new copies of those files, but with new random names. These new files would test a process which needed a lot of files to be loaded and the files had to be of a specific format and data such as a zip archive.

gci {TargetDirectory} -file  | Copy-Item -Destination { "$([System.IO.Path]::GetRandomFileName()).{Target Extension}" } -whatif

  • This operation copies the file to the current directory.
  • The GetRandomFileName will generate both random text and random lettered extensions.

I needed my files to be all of the same type so I this is the format I used:

gci C:\Test\Initial -file  | Copy-Item -Destination { "$([System.IO.Path]::GetRandomFileName()).zip" } -whatif

Share

Leave a Reply