1

I am trying to determine what Powershell command would be equivalent to the following Linux Command for creation of a large file in a reasonable time with exact size AND populated with the given text input.

New Question asked as this one was automatically closed for some reason.

Given:

$ cat line.txt
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ZZZZ

$ time yes `cat line.txt` | head -c 10GB > file.txt  # create large file
real    0m59.741s

$ ls -lt file.txt
-rw-r--r--+ 1 k None 10000000000 Feb  2 16:28 file.txt

$ head -3 file.txt
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ZZZZ
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ZZZZ
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ZZZZ

What would be the most efficient, compact Powershell command which would allow me to specify the size, text content and create the file as the Linux command above? Thanks!

mojoa
  • 103
  • 1
  • 8

1 Answers1

5

Fsutil still exists.

In PS, it goes like this:

$file = [System.IO.File]::Create("C:\users\Me\Desktop\test.txt") #input path here
$file.SetLength([double]10mb) #Change file size here
$file.Close()

Its really just using raw data types.

Id recommend to just throwing it into a function if you're going to continue doing this more often:

#Syntax:
# Create-File [-Path] <string> [-Size] <string>
#Example:
# Create-File -Path c:\Users\me\test.txt -Size 20mb
Function Create-File{
    Param(
        [Parameter(Mandatory=$True,Position=0)]
        $path,

        [Parameter(Mandatory=$True,Position=1)]
        $size)

$file = [System.IO.File]::Create("$path")
$file.SetLength([double]$size)
$file.Close()
}

Fsutil is used like fsutil file createnew filename filesize, based off this link: here

EDIT: Guess you can just add the Type using powershell like so:

$file = new-object System.IO.FileStream c:\users\me\test.txt, Create, ReadWrite
$file.SetLength(10MB)
$file.Close()
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • This ignores the "text content" requirement of the original question – andreyrd Feb 16 '23 at 14:03
  • @andreyrd, for that you can use [`Set-Content`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content?view=powershell-7.3). – Abraham Zinala Feb 16 '23 at 18:46