Windows in-built compression command without utilizing any external tools
You don't need to be an expert script writer to utilize PowerShell and you do not need to utilize an explicit PowerShell script (or its IDE interface) to use PowerShell commands—you can utilize PowerShell commands and make them part of the batch script logic to execute and use.
I think you are looking for a Windows native way to complete the task, and you just want to ensure it is indeed native to Windows and will work on from Windows 7 and above (newer).
Compressing and Archiving
I've created a batch script below that works from Windows 10 and Windows 7 and uses logic that works on older versions of PowerShell as well as newer versions just in case I used older logic.
Drag and Drop
Just drag the folder you wish to compress to the batch script and it'll be archived as a zip file and it will create the new zip file in that same location but with the
<foldername you archive>.zip
so if you dragC:\User\User\Desktop\New Folder
into the script, it will createC:\User\User\Desktop\New Folder.zip
Drag folder to script
Created afterwards
The Script
Note: I've left two variables in the script commented out up top so you can use those and set the source folder and the destination file name explicitly if you wish rather than using the drag and drop method.
Source link to the PowerShell logic idea
@ECHO ON
::SET src_folder=C:\Users\User\Desktop\Test
::SET destfile=C:\Users\User\Desktop\Test.zip
SET src_folder=%~1
SET destfile=%~FN1.zip
:DynamicPSScriptBuild
SET PSScript=%temp%\%~n0.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
ECHO [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")>>"%PSScript%"
ECHO $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal >>"%PSScript%"
ECHO $includebasedir = $true >>"%PSScript%"
ECHO [System.IO.Compression.ZipFile]::CreateFromDirectory("%src_folder%", "%destfile%", $compressionLevel, $includebasedir)>>"%PSScript%"
:PowerShell
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
EXIT
Decompressing and Extracting
I've also created a batch script below that works from Windows 10 and Windows 7 and uses logic that works on older versions of PowerShell as well as newer versions just in case I used older logic.
Drag and Drop
Just drag the zip file want to decompress into the script and it will extract the contents of the file to the same folder as the zip file you dragged into the script resides. So if you have
C:\User\User\Desktop\ZipMe.zip
and it contains a folder namedZipMe
, once you drag and drop it into the script, it will extract the folderZipMe
toC:\User\User\Desktop
.Drag the zip file to the script
Extracted afterwards
The Script
Important: The variable in the Compressing and Archiving script above (the first script up top) shown as $includebasedir = $true
tells it to zip the actual folder and the contents within. If this value was set to $false
instead or if you extract from a zip that does not contain the folder, the below script will extract all the files and other contents to the same folder the zip file resides—so 100 files and folders will be extracted if that's what the zip file contained with no containing parent folder within the file.
Note: I've left two variables in the script commented out up top so you can use those and set the destination folder and the zip file name explicitly if you wish rather than using the drag and drop method.
@ECHO ON
::SET dest_folder=C:\Users\User\Desktop\New folder
::SET zip_file=C:\Users\User\Desktop\Test\test.zip
SET dest_folder=%~DP1
SET zip_file=%~1
:DynamicPSScriptBuild
SET PSScript=%temp%\%~n0.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
ECHO [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.Filesystem")>>"%PSScript%"
ECHO [io.compression.zipfile]::ExtractToDirectory("%zip_file%", "%dest_folder%") >>"%PSScript%"
:PowerShell
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
GOTO :EOF
Further Resources
- CreateFromDirectory Method (String, String, CompressionLevel, Boolean)
- ExtractToDirectory Method (String, String)
At least Windows 7 (and XP... I'm not quite as sure about the state with Windows 10 yet) doesn't have a command line interface to work with the ZIP compression support provided by "Microsoft Compressed Folders" support (like what Explorer can use).
However, you can do this by simply creating a script file, and then using Windows Script Host, which is built into Windows XP and later (as is the "Microsoft Compressed Folders" support).
For instance, Peter Mortensen's answer shows the contents of a batch file that can be made, which writes out 8 lines of VBScript which then utilize the "Microsoft Compressed Folders" support.
Note that the "sleep 2000" at the end just pauses 2 seconds, and isn't nearly as safe of an approach as George Yockey's approach (checking for active compressing) or checking .Count (something like kayleeFrye_onDeck's submission).
I'm not sure if a WSH-based answer violates the goals you intended when you said you didn't want PowerShell, but that indeed does not use PowerShell at all.