In previous tips we illustrated how you can use PowerShell to split files into smaller file chunks, and how to join the chunks to recreate the original file. We expanded these functions even more and published them to the PowerShell Gallery, so to split and join files, just grab the module and install it like this:

 
PS> Install-Module -Name FileSplitter -Repository PSGallery -Scope CurrentUser -Force
 

Now whenever you need to split a large file into smaller parts, run this:

 
PS C:\> Split-File -Path 'C:\movies\Woman tries putting gas in a Tesla.mp4' -PartSizeBytes 10MB -AddSelfExtractor -Verbose
VERBOSE: saving to C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.0.part...
VERBOSE: saving to C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.1.part...
VERBOSE: saving to C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.2.part...
VERBOSE: Adding extractor scripts...

PS C:\
 

Split-Path splits the file into smaller parts no larger than specified in -PartSizeByte. Thanks to -AddSelfExtractor, it also adds a script that can be used to join the part files back to the original file, and a shortcut file that launches the extractor file on double-click. Here are the files you get:

 
PS C:\users\tobwe\Downloads> dir *gas*


    Folder: C:\movies


Mode                LastWriteTime         Length Name                                                                                                                    
----                -------------         ------ ----                                                                                                                    
-a----       03.03.2019     18:11           2004 Extract Woman tries putting gas in a Tesla.mp4.lnk                                                                      
-a----       03.03.2019     16:54       24081750 Woman tries putting gas in a Tesla.mp4                                                                                  
-a----       03.03.2019     18:11       10485760 Woman tries putting gas in a Tesla.mp4.0.part                                                                           
-a----       03.03.2019     18:11       10485760 Woman tries putting gas in a Tesla.mp4.1.part                                                                           
-a----       03.03.2019     18:11        3110230 Woman tries putting gas in a Tesla.mp4.2.part                                                                           
-a----       03.03.2019     18:11           3179 Woman tries putting gas in a Tesla.mp4.3.part.ps1
 

As you can see, there are a number of files with .part extension, plus one with .part.ps1 extension. The latter is the extraction script. When you run this, it takes the parts and recreates the original file, then deletes all part files plus itself. At the end, the extractor script opens the File Explorer and selects the restored file.

Since it might not be obvious to everyone how to run a PowerShell script, there is an additional file with .lnk extension called “Extract…”. This is a shortcut file. When a user double-clicks this file, it runs the PowerShell extractor script and restores the original file.

If you’d rather like to manually restore the original file, you can call Join-File yourself:

 
PS> Join-File -Path 'C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4' -Verbose -DeletePartFiles
VERBOSE: processing C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.0.part...
VERBOSE: processing C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.1.part...
VERBOSE: processing C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.2.part...
VERBOSE: Deleting part files...

PS>
 

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU - with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Anonymous