PowerShell Random File Generator

PowerShell random file generator Sometimes you are in need to generate files with random names and/or size, while on the internet you can find some small programs doing exactly this you can leverage some .Net functions and the fsutil  so to turn PowerShell in a random file generator.

Here’s a quick script I often used to generate random files which comes in handy when I need, for example, to stuff lab user’s mailboxes with data:

 

#PowerShell Random File Generator

# Set the initial value to control the do loop
$seed = 0

# How many files should be generated
$random = 1

# File size in bytes
$ranSize = 4096

# Path where the file will be created
$ranPath = "C:temp"

do {
    $netFn = [System.IO.Path]::GetRandomFileName();

    fsutil file createnew $ranPath$netFn $ranSize;
    $seed++;
}

#Generate the defined number of files
until ($seed -eq $random);

Save the code to a convenient location with a name like PowerShell Random generator.ps1 and run it, as I said this is kind of private script I use for testing purposes so I kept the code clean and simple, but I would love to hear any suggestion or enhancement from you so leave a comment below and don’t forge to subscribe to RSS.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s