There is no direct PowerShell equivalent of your command.
In fact, with files of this size your best bet is to avoid PowerShell's own cmdlets and pipeline and to make direct use of .NET types instead:
& {
param($outFile, $size, $content)
# Add a newline to the input string, if needed.
$line = $content + "`n"
# Calculate how often the line must be repeated (including trailing newline)
# to reach the target size.
[long] $remainder = 0
$iterations = [math]::DivRem($size, $line.Length, [ref] $remainder)
# Create the output file.
$outFileInfo = New-Item -Force $outFile
$fs = [System.IO.StreamWriter] $outFileInfo.FullName
# Fill it with duplicates of the line.
foreach ($i in 1..$iterations) {
$fs.Write($line)
}
# If a partial line is needed to reach the exact target size, write it now.
if ($remainder) {
$fs.Write($line.Substring(0, $remainder))
}
$fs.Close()
} file.txt 1e10 (Get-Content line.txt)
Note: 1e10
uses PowerShell's support for scientific notation as a shorthand for 10000000000
(10,000,000,000
, i.e., [Math]::Pow(10, 10
). Note that PowerShell also has built-in support for byte-multiplier suffixes - kb
, mb
, gb
and tb
- but they are binary multipliers, so that 10gb
is equivalent to 10,737,418,240
(10 * [math]::Pow(1024, 3)
), not decimal 10,000,000,000
.
Note:
The size passed (1e10
in this case) is a character count, not a byte count. Given that .NET's file I/O APIs use BOM-less UTF-8 encoding by default, the two counts will only be equal if you restrict the input string to fill the file with to characters in the ASCII range (code points 0x0 - 0x7f
).
The last instance of the input string may be cut off (without a trailing newline) if the total characters count isn't an exact multiple of the input string length + 1 (for the newline).
Optimizing the performance of this code by up to 20% is possible, through a combination of writing bytes and output buffering, as shown in zett42's helpful answer.
The above performs reasonably well by PowerShell standards.
In general, PowerShell's object-oriented nature will never match the speed of the raw byte handling provided by native Unix utilities / shells.
It wouldn't be hard to turn the code above into a reusable function; in
a nutshell, replace & { ... }
with something like function New-FileOfSize { ... }
and call New-FileOfSize file.txt 1gb (Get-Content line.txt)
- see the conceptual about_Functions help topic, and about_Functions_Advanced for how to make the function more sophisticated.