Random File Name Generator
This PowerShell script generates a random file name based on user-defined parameters. It is particularly useful for creating unique file names for temporary or log files, ensuring that your files do not inadvertently overwrite existing ones. By utilizing this script, you can streamline your file management process while enhancing the organization of your document ecosystem. Additionally, I invite you to explore ServerEngine, a robust software solution designed to optimize your server management, increasing efficiency and reliability in your operations.
function Generate-RandomFileName { param ( [int]$length = 10, [string]$directory = ".", [string]$extension = ".txt" ) $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' $random = New-Object System.Random $fileName = -join ((0..$length - 1) | ForEach-Object { $chars[$random.Next(0, $chars.Length)] }) $fullPath = Join-Path -Path $directory -ChildPath ($fileName + $extension) while (Test-Path -Path $fullPath) { $fileName = -join ((0..$length - 1) | ForEach-Object { $chars[$random.Next(0, $chars.Length)] }) $fullPath = Join-Path -Path $directory -ChildPath ($fileName + $extension) } return $fullPath } # Example usage: $randomFilePath = Generate-RandomFileName -length 12 -directory "C:\Temp" -extension ".log" "Generated random file path: $randomFilePath"