Search

shirishweb

Your right place for "Right to Know"

Category

Powershell

PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language. PowerShell is a very powerful tool for automating various windows tasks. Here we will learn to use powerShell as a commandline tool for creating archives (zip) files from folder containing files.

I am using windows 10 pc which comes with powershell version 5.1

I have a folder structure as follows.

I have a folder called Software Documentations with contains 7 files.

Using the explorer address bar, we can start a new powershell session using this current directory.

Since powershell leverages use of .net classes, we will use “System.IO.Compression.FileSystem” class and load it in powershell session as

Add-type -A System.IO.Compression.FileSystem

We will create instance of this class in a powershell variable called $cls

$cls = [IO.Compression.ZipFile]

Now lets see what static functions are provided by our ZipFile class

$cls | Get-Member -static

We can see that we have methods “CreateFromDirectory” and “ExtractToDirectory” which we are interested on.

Now lets begin creating zip file using method CreateFromDirectory

First we need to make sure we are in correct directory

ls

Lets save the directory path of our desired folder to be zipped (which is “3. Software Requirement Specification” in my case”) into powershell variable $source

$source = (gi '3. Software Requirement Specification').FullName

Make sure $source has some value in it

$source

Now lets use our static “CreateFromDirectory” method to convert this folder to a zip file called “myzip.zip”

$cls::CreateFromDirectory($source, "myzip.zip")

Yes, we verified we have a our zip file “myzip.zip” in the same directory.

Now lets demonstrate extracting same zipped file to a new folder “myzip_extracted” folder using “ExtractToDirectory” method provided by our $cls

$cls::ExtractToDirectory('myzip.zip', 'myzip_extracted')

And we can see that it worked and created myzip_extracted folder and put all my files from the zipped archive to this folder.

Advertisements

Hunting for processes holding network port

Many times, we may encounter certain errors within the application showing some strange errors with network issues. Like XAMPP showing error when apache is started complaining that the port is already in use. In such case, we need to find which service/program is using the port and reconfigure the application/service as per the requirement.

Today I will be showing how to hunt for such process using the powershell in windows system step by step. Lets hunt for process holding the network port 80.

  • First start run and type in powershell to start powershell terminal1. Powershell in run
  • Run this command

start powershell -verb runas

2. Start powershell in elevated mode

  • to start another powershell terminal in elevated [administrator privileged mode]
  • Run command

netstat -aonb

3. Netstat command

  • to view all the listening / established / connected ports with details
  • Since this is a long list, looking for specific port might be difficult, so we cant search this list output by using command

    netstat -aonb | findstr :80

5. Grep 80 port from netstat

  • It shows that the process with PID 4088 is holding that port.
  • Now lets find the process with that PID ie 4088. To do this, we can any of two commands as

tasklist | findstr 4088

OR

Get-Process -PID 4088

6. Grep process with pid

  • Voila, we have hunted the process we are looking for. Now we know that port 80 is used by process httpd.exe.

Create a free website or blog at WordPress.com.

Up ↑