Creating Multiple Files with Content Using PowerShell

This post aims to explain a straight forward yet effective PowerShell functionality which can help the user in creating more than one file with specified content.

This can be useful for developers, system administrators or any user who wishes to generate files quickly for testing purposes or something else.

The PowerShell Function

Here’s a PowerShell function that allows you to create a specified number of files in a folder, with the option to include custom content.

If no content is provided, the function will fill the files with default “Lorem ipsum” text.

function New-Files {
    param (
        [string]$FolderPath,               # Path of the folder where files will be created
        [int]$FileCount = 10,              # Number of files to create
        [string]$FileExtension = "txt",    # File extension (without the dot)
        [string]$Content = $null           # Content to write inside each file; defaults to $null
    )

    # Default bogus content if Content is not provided or is empty
    if (-not $Content) {
        $Content = @"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
"@
    }

    # Ensure the folder exists; if not, create it
    if (-not (Test-Path -Path $FolderPath)) {
        $null = New-Item -ItemType Directory -Path $FolderPath -Force
    }

    # Generate the specified number of files with the specified or default content
    for ($i = 1; $i -le $FileCount; $i++) {
        $FileName = "File_$i.$FileExtension"
        $FullPath = Join-Path -Path $FolderPath -ChildPath $FileName
        
        New-Item -ItemType File -Path $FullPath -Force | Out-Null
        Set-Content -Path $FullPath -Value $Content

        # Update the progress bar
        $PercentComplete = [math]::Round(($i / $FileCount) * 100)
        Write-Progress -Activity "Creating Files" -Status "Creating $FileName" -PercentComplete $PercentComplete
    }
}

Function Parameters

  • FolderPath: The path where the files will be created. If the folder does not exist, it will be automatically created.
  • FileCount: The number of files to create. The default value is 10.
  • FileExtension: The extension of the files (without the dot). The default is txt.
  • Content: The content to be written inside each file. If this parameter is left empty, the function will use a default “Lorem ipsum” text as a placeholder.

Using the Function

To use the function, you can call it with the desired parameters like so:

# Create 5 files with the default content in the C:\Temp folder
New-Files -FolderPath "C:\Temp" -FileCount 5

This will create 5 files in the C:\Temp folder with the default “Lorem ipsum” content.

You can also specify custom content for the files:

# Create 3 files with custom content in the C:\Temp folder
New-Files -FolderPath "C:\Temp" -FileCount 3 -Content "This is a custom content."

This will create 3 files in the C:\Temp folder with the specified custom content.

Why This Function Is Useful

This function simplifies the task of generating multiple files with consistent content, which can be especially useful for:

  • Testing: Creating sample files for testing file operations, batch processing, or scripts.
  • Automation: Automating repetitive tasks where multiple files with similar content are needed.
  • Organization: Quickly generating placeholder files during the initial stages of a project.

Conclusion

PowerShell is a powerful tool for automation, and this function is a small example of how you can leverage it to save time and streamline your workflow. Feel free to customize this function to suit your specific needs, and don’t hesitate to share your modifications!

Happy scripting!