Solved

Compress a file via PowerShell

Posted on 2014-04-07
5
1,576 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
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 34

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 34

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now