Solved

Compress a file via PowerShell

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
A brief introduction to what I consider to be the best editor for PowerShell.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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