none
powershell output copied to other command in script RRS feed

  • Question


  • im trying to make a  script that detects max free space  on drive  in bytes then copies the bytes number to  other command spot before running  2nd command

    need  the output bytes to  be copied to   file creator section for bytes that says 1073741824

    allso like to have in  it is a perimeter to  do a percentage of free space to fill  as a perimeter  if i dont want to do all free space by  lets say  50% of it 

    so far only found  this command

    Get-WMIObject Win32_Logicaldisk -filter "deviceid='C:'"


    i have file creator


    $out = new-object byte[] 1073741824; (new-object Random).NextBytes($out); [IO.File]::WriteAllBytes('C:\temp\mypowerfile.txt', $out)


    thanks
    Tuesday, July 7, 2020 11:40 PM

All replies

  • The largest array possible with this command is an [int32]::MaxValue.

    To get the value of the free space:

    $numbytes = (Get-WMIObject Win32_Logicaldisk -filter "deviceid='C:'").Freespace

    To use this it must be less than the length of an int32.  To write more you need to write successive blocks until you reach the size you need.  It is likely easier to just create a file at length.


    \_(ツ)_/

    Tuesday, July 7, 2020 11:51 PM
    Moderator
  • Tuesday, July 7, 2020 11:57 PM
    Moderator
  • Even easier (no need for admin privileges like fsutil) -- use sysinternals 'contig' tool.

    https://docs.microsoft.com/en-us/sysinternals/downloads/contig

    Use the "-n" parameter followed by the file name and desired file length.

    If you want to create that file with PowerShell (example leaves 10MB of free space on the disk):

    [io.file]::Create("C:\junk\giantfile.txt").SetLength((Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace - 10MB).Close


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Wednesday, July 8, 2020 1:49 AM
  • Again - [IO.FILE]::Create cannot create a file greater than 2Gb.  On todays disks the free space will nearly always be greater than 2Gb.


    \_(ツ)_/

    Wednesday, July 8, 2020 1:56 AM
    Moderator
  • 2GB?! Darned facts! They're stubborn, aren't they?

    Try this, but uncomment the lines that create the files! I left them as comments so you can verify that the code works.  You can leave the progress lines if you want, but be aware that they'll send strings to the output stream.

    Note that I haven't tried to cover any "corner cases" (like asking to fill 0% of the freespace, or checking for errors!)

    function GobbleSpace{
        param(
            [double]$FillToThisPct=100.0,
            $CreateFilesHere="C:\Junk\",
            $FileName="GiantFile"
        )
        $DriveLetter = $CreateFilesHere.Substring(0,1)
        $FreeSpace = (Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='${DriveLetter}:'").FreeSpace
        $ConsumeThisMuch = $FreeSpace * ($FillToThisPct / 100.0)
        $FileSuffix=0
        While ($ConsumeThisMuch -ge [int32]::MaxValue){
            $FileSuffix++
            #[io.file]::Create("$CreateFilesHere$FileName$FileSuffix.bin").SetLength([int32]::MaxValue).Close
            "Created file $CreateFilesHere$FileName$FileSuffix.bin : Size $([int32]::MaxValue)"
            $ConsumeThisMuch -= [int32]::MaxValue
        }
        if ($ConsumeThisMuch -gt 0){
            $FileSuffix++
            #[io.file]::Create("$CreateFilesHere$FileName$FileSuffix.bin").SetLength($ConsumeThisMuch).Close
            "Created file $CreateFilesHere$FileName$FileSuffix.bin : Size $ConsumeThisMuch)"
        }
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, July 8, 2020 2:56 PM
  • 2GB?! Darned facts! They're stubborn, aren't they?

    Try this, but uncomment the lines that create the files! I left them as comments so you can verify that the code works.  You can leave the progress lines if you want, but be aware that they'll send strings to the output stream.

    Note that I haven't tried to cover any "corner cases" (like asking to fill 0% of the freespace, or checking for errors!)

    function GobbleSpace{
        param(
            [double]$FillToThisPct=100.0,
            $CreateFilesHere="C:\Junk\",
            $FileName="GiantFile"
        )
        $DriveLetter = $CreateFilesHere.Substring(0,1)
        $FreeSpace = (Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='${DriveLetter}:'").FreeSpace
        $ConsumeThisMuch = $FreeSpace * ($FillToThisPct / 100.0)
        $FileSuffix=0
        While ($ConsumeThisMuch -ge [int32]::MaxValue){
            $FileSuffix++
            #[io.file]::Create("$CreateFilesHere$FileName$FileSuffix.bin").SetLength([int32]::MaxValue).Close
            "Created file $CreateFilesHere$FileName$FileSuffix.bin : Size $([int32]::MaxValue)"
            $ConsumeThisMuch -= [int32]::MaxValue
        }
        if ($ConsumeThisMuch -gt 0){
            $FileSuffix++
            #[io.file]::Create("$CreateFilesHere$FileName$FileSuffix.bin").SetLength($ConsumeThisMuch).Close
            "Created file $CreateFilesHere$FileName$FileSuffix.bin : Size $ConsumeThisMuch)"
        }
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

     im learning  to read and write  powershell can you explain functions in your script and  anything i have to configure
    Wednesday, July 8, 2020 8:44 PM
  • im new to programming so im  trying to learn 

    if i understand right  there no way to do  more then 2 Gb size file 

     is there a way i can create  many 2Gb files  to fill in all free space
    Wednesday, July 8, 2020 8:47 PM
  • We cannot provide private instruction as it is beyond the scope and resources of this forum.

    Here is a good place to start and it is free,


    \_(ツ)_/

    Wednesday, July 8, 2020 9:17 PM
    Moderator
  • Yes. Use the function "GobbleSpace" I provided in this thread. But please be careful . . . if you completely fill a disk such as the system/boot disk you may prevent the system from working until you free up space!

    You can create a file of any size you like . . . just open file stream and write to it repeatedly in, say, 4MB chunks (or 2GB chunks). You can use the Add-Content to do this. Check the available free space on the disk before each write to be sure that there's enough room to write the data!


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, July 8, 2020 9:28 PM
  • The function just gets the amount of free space available on the disk and multiplies that by the percentage of that space you want to fill. It then stays in a loop, creating files of 2GB until there's no longer 2GB of space left in the quota you specified. Then it writes on last file of sufficient size (if necessary) to reach the limit you imposed.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, July 8, 2020 9:32 PM
  • Rich Matheisen  in your script what  locations/lines do  i have to edit to do less then 100.0% free space because see 2 locations with  100.0

    and how do i change drive name and location  and folder

    please give me line of code and location  so i dont edit wrong line

     

    do i run  it by  running file with script?

    thanks




    • Edited by sniperjul Thursday, July 9, 2020 10:22 PM
    Thursday, July 9, 2020 10:20 PM
  • At the rate you guys are going you will end up rewriting Windows and the Magna Carta before you are done.

    Why not just use one command to do this.

    fsutil 

    It is part of Windows and has been since the earliest days.  It can create a file of any size in one line.

    PS C:\scripts> fsutil file createnew /?
    Usage : fsutil file createNew <filename> <length>
       Eg : fsutil file createNew C:\testfile.txt 1000
    PS C:\scripts>



    \_(ツ)_/

    Thursday, July 9, 2020 10:45 PM
    Moderator
  • At the rate you guys are going you will end up rewriting Windows and the Magna Carta before you are done.

    Why not just use one command to do this.

    fsutil 

    It is part of Windows and has been since the earliest days.  It can create a file of any size in one line.

    PS C:\scripts> fsutil file createnew /?
    Usage : fsutil file createNew <filename> <length>
       Eg : fsutil file createNew C:\testfile.txt 1000
    PS C:\scripts>


    fsutil only creates spare files /null that only allocates file no data is written to disk  

    need files with byte data in them 





    • Edited by sniperjul Thursday, July 9, 2020 11:38 PM
    Thursday, July 9, 2020 11:32 PM

  • fsutil only creates spare files /null that only allocates file no data is written to disk  





    What's the real problem? What are you really trying to accomplish? Are you trying to overwrite the disk sectors before you recycle the drives so that data cannot be recovered?

    Why not just use a tool like eraser? https://eraser.heidi.ie/

      
    Friday, July 10, 2020 12:00 AM

  • fsutil only creates spare files /null that only allocates file no data is written to disk  





    What's the real problem? What are you really trying to accomplish? Are you trying to overwrite the disk sectors before you recycle the drives so that data cannot be recovered?

    Why not just use a tool like eraser? https://eraser.heidi.ie/

      

    That is what the OP asked for.  A file full of nulls.  Look at the original post.

    The SysInternals tool can write patterns.  The SysInternals SDELETE tool can write patterns to all of the dick including unused areas of the directory and other control files. 

    To fill a 4 Tb disk to 50% by write small blocks can take hours.  It all depends on the purpose for this.  Choosing a method without a purpose is just dumb.  Why waste time doing something that may not work. Always define the parameters of the problem before choosing a solution.  Untrained people always do this backwards which means they most often do unnecessary things or don't do needed things.

    None of the above is rocket science.  It is just the most basic rules of system design for all technologies and sciences.


    \_(ツ)_/

    Friday, July 10, 2020 12:16 AM
    Moderator

  • That is what the OP asked for.  A file full of nulls.  Look at the original post.


    The original post does not define the problem. It asks for a script to write to a file. He also mentions only writing to 50% of all free space. 

    I'm trying to make a script that detects max free space on drive in bytes then copies the bytes number to other command spot before running 2nd command need the output bytes to be copied to file creator section for bytes that says 1073741824 allso like to have in it is a perimeter to do a percentage of free space to fill as a perimeter if i dont want to do all free space by lets say 50% of it so far only found this command Get-WMIObject Win32_Logicaldisk -filter "deviceid='C:'" i have file creator $out = new-object byte[] 1073741824; (new-object Random).NextBytes($out); [IO.File]::WriteAllBytes('C:\temp\mypowerfile.txt', $out) thanks

    This is an XY problem. Had the OP initially asked; "What is the best tool to use to wipe out the data on a hard drive?", instead of asking for a script, this question might have been answered on Tuesday. While we can assume what the intent of the post is, unless the OP asks the question, we don't know for sure.   

    If I had a disk with sensitive data, I would first reformat it, then use something like Eraser to overwrite the files. And if I really wanted it gone, I would physically destroy the media. 

    Friday, July 10, 2020 1:08 AM
  • His example shows he is writing an empty buffer to the file.  That is a buffer full of nulls.

    50% only requires dividing the free space by 2.

    fsutil is the easiest way to do what is asked in the original question unless the OP states what the purpose is.  Any other answer would be based on guessing.


    \_(ツ)_/

    Friday, July 10, 2020 1:15 AM
    Moderator
  • You don't have to change anything. Just supply the value using the parameter "-FillToThisPct" when you call the function.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, July 10, 2020 2:03 AM
  • is    file name /fill rate  /drive path  perimeters  shown when i run  script so i can change it

    and how do i  fill less free space  when i want too  

    please  explain so can  understand  better

    my goal is to write real data to disk to fill in free space


    Rich Matheisen thanks you for helping me 

















    • Edited by sniperjul Saturday, July 11, 2020 8:56 PM
    Friday, July 10, 2020 2:36 AM
  • Rich Matheisen  can you please reply  to above post  
    Monday, July 13, 2020 10:31 PM
  • Nothing is "shown" to you. You supply the parameters when you use the function.

    FYI, the "drive path" is taken from the first character of the parameter value "CreateFilesHere".

    If you intend to "write real data" to fill the space (the function doesn't do that) you should be prepared to wait for a long time. You should also consider that the length data you write will depend on the encoding of the data written to the file. Nobody knows what you mean by "real data", or how that data will be encoded.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Monday, July 13, 2020 10:48 PM
  •  so  if i wanted different drive i would change path

    param(
            [double]$FillToThisPct=100.0,
            $CreateFilesHere="C:\Junk\",
            $FileName="GiantFile"

    my concern  is dont want to fill whole disk   confused how do i  reduce it size  

    real data i mean  bytes  to disk  binary data  like if i wanted to write   word or digits/letters  like all F's or random data to disk  

    like a file    filled with all  1"s  like if i write hex 31 to disk it comes out as all 1's as symbol


    i wanted to create something in powershell  if possible




    • Edited by sniperjul Tuesday, July 14, 2020 10:50 PM
    Tuesday, July 14, 2020 10:26 PM
  • To fill less than 100% of the available free space just supply whatever percentage you want to fill by supplying the percentage by using the "-FillToThisPct" parameter when you call the function.

    If you want to change the drive, first create a directory that the function will write the files into on whatever drive you want. Then supply the path (with the trailing "/") using the parameter "-CreateFilesHere". The function writes the files into that patch so you can just delete the directory when you're ready to free up the space on the disk.

    That function was written just to demonstrate how to accomplish what you asked by using PowerShell. It wasn't meant to be used as-is because there's no error checking in it, nor is there anything in it to prevent you from actually filling the entire disk and perhaps stopping your system. Use it to learn from, then write one that does all the things you need it to do.

    FYI, the function creates one, or more files, that will be filled by null values (i.e. hexadecimal 00) 


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, July 15, 2020 2:01 AM
  • i had tried to run  it but it would not run   i  even  changed powershell  policy to unrestricted 

    i had code in txt file so i renamed  it with  ps1 extension  tried to run it from file  didn't work 

    only thing i changed was  [double]$FillToThisPct=100.0 to 10.0

    i had file   directory location same as  in script

    im in windows 10 home  admin  rights

    Friday, July 17, 2020 8:12 PM
  • You're going to have to be more specific than "it would not run".

    Firstly, it's a function. You have to call the function from your own code. So, open your ps1 file and add this below the last line:

        GobbleSpace -FillToThisPct 10.0

    Assuming you're working with the "C:" drive and there's a directory named "Junk" in the root directory of that drive, that's all you need to do.

    There's no need to change anything in the function's code -- it's all parameterized.

    You truly need to learn the PowerShell language and general programming concepts if you're having trouble understanding things such as this. JRV gave you several links as a reference. Please, at least read the Windows PowerShell book (several times if necessary).

    https://www.sapien.com/books_training/Windows-PowerShell-4


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, July 17, 2020 9:21 PM
  • i dont know what is required for  it to run  correctly  and missing 

    need your help

    i'm trying to learn but not easy if i dont know what is missing or required 

    function GobbleSpace{
        param(
            [double]$FillToThisPct=100.0,
            $CreateFilesHere="C:\Junk\",
            $FileName="GiantFile"
        )
        $DriveLetter = $CreateFilesHere.Substring(0,1)
        $FreeSpace = (Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='${DriveLetter}:'").FreeSpace
        $ConsumeThisMuch = $FreeSpace * ($FillToThisPct / 100.0)
        $FileSuffix=0
        While ($ConsumeThisMuch -ge [int32]::MaxValue){
            $FileSuffix++
            #[io.file]::Create("$CreateFilesHere$FileName$FileSuffix.bin").SetLength([int32]::MaxValue).Close
            "Created file $CreateFilesHere$FileName$FileSuffix.bin : Size $([int32]::MaxValue)"
            $ConsumeThisMuch -= [int32]::MaxValue
        }
        if ($ConsumeThisMuch -gt 0){
            $FileSuffix++
            #[io.file]::Create("$CreateFilesHere$FileName$FileSuffix.bin").SetLength($ConsumeThisMuch).Close
            "Created file $CreateFilesHere$FileName$FileSuffix.bin : Size $ConsumeThisMuch)"
        }
    }

     

    • Edited by sniperjul Sunday, July 19, 2020 8:46 PM
    Sunday, July 19, 2020 8:43 PM
  • Perhaps you should chose a more elementary project while you learn PowerShell?

    There are myriad videos on YouTube with topics such as "Microsoft PowerShell for Beginners", "Learn PowerShell in a Month of Lunches", "Powershell Master Class", etc. Just search YouTube for "PowerShell". The "Getting Started With PowerShell 3.0 Jump Start" is also there (from Microsoft).


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Sunday, July 19, 2020 9:43 PM