Solved

Compress a file via PowerShell

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
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…
how to add IIS SMTP to handle application/Scanner relays into office 365.

809 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