Solved

Compress a file via PowerShell

Posted on 2014-04-07
5
1,640 Views
Last Modified: 2014-04-08
Hi Experts,

Is is possible to zip a file via PowerShell without installing 7-zip or any third-party tools?

My script currently generates a TXT and I need to zip in the end.

Here it is the script

###########Script developed by me 07 Feb 2014 to generate an AD Users Info of Company########
Import-Module ActiveDirectory
Get-ADObject -filter 'objectclass -eq "user"' -properties * | select > "C:\scripts\ExportADusersInfo.txt"
###########End of Script


This txt file is just too big to be emailed internally in my organization. I would need to zip it before the next script sends it.


Best Regards
0
Comment
Question by:osagarana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Expert Comment

by:xeroxzerox
ID: 39985177
you can use compact command in script

Syntax
compact /parameters FileName

Parameters
/c : Compresses the specified directory or file.
/u : Uncompresses the specified directory or file.
/s:dir : Specifies that the requested action (compress or uncompress) be applied to all subdirectories of the specified directory, or of the current directory if none is specified.
/a : Displays hidden or system files.
/i : Ignores errors.
/f : Forces compression or uncompression of the specified directory or file. This is used in the case of a file that was partly compressed when the operation was interrupted by a system crash. To force the file to be compressed in its entirety, use the /c and /f parameters and specify the partially compressed file.
/q : Reports only the most essential information.
FileName : Specifies the file or directory. You can use multiple file names and wildcard characters
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39985202
What version of PS and .NET are you using?
0
 
LVL 11

Expert Comment

by:Tej Pratap Shukla ~Dexter
ID: 39985211
Hey there,
I was aware of zipping techniques using compression utilities like 7zip but I looked up some sources and found this code.
 function ZipFiles( $zipfilename, $sourcedir )
{
   [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
   $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
   [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
        $zipfilename, $compressionLevel, $false)
}
Provide the full source path , zip file name , compression level  and the full path to the directory containing the files you would like to zip.

Thanks
~Dex
0
 
LVL 1

Author Comment

by:osagarana
ID: 39987676
I gave up with the options of compressing a file without a third-party tool.
I just copied 7z.exe + 7z.dll to my folder.

So on my script above I would like to add this line >>

7z a -tzip ExportADusersInfo.zip ExportADusersInfo.txt

Does anyone know how I could input the code above to zip the file in the same script?

Cheers
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39987874
Can't you simply use:
Import-Module ActiveDirectory
Get-ADObject -filter 'objectclass -eq "user"' -properties * | select > "C:\scripts\ExportADusersInfo.txt"
& C:\scripts\7z.exe a -tzip C:\scripts\ExportADusersInfo.zip C:\scripts\ExportADusersInfo.txt

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question