Solved

Compress a file via PowerShell

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. Theā€¦
how to add IIS SMTP to handle application/Scanner relays into office 365.

705 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

18 Experts available now in Live!

Get 1:1 Help Now