Important commands in Windows for Professionals (Part – 1)

Hi everyone, in this post I will explore some important commands in windows., so let’s start the journey.
The reason that I have written this post is to get familiarity about the important commands that can be used to automate tasks.

The commands that i will be exploring in this post are
1) taskkill
2) net stop | net start
3) appcmd start sites | appcmd stop sites
4) hostname
5) shutdown

Note – We can get help about the commands directly from the command prompt itself by typing
command_name -? .

1) taskkill

This command is used to terminate tasks either by name (image name) or process id (PID).

Syntax:  taskkill {–PID Process_ID | -IM image_name}

When we open a notepad application a process is started the image name is usually "notepad.exe" and the process id is some number.
If we open the notepad application again a new process is started with the same image name “notepad.exe”, here the image name is same as the previous image name but for this instance the process id will be different.

We can see the image name and the process id from the process tab of the task manager.


Task Manager

When we terminate a process by its image name, then all the process with that image name will be terminated 

Note - Each Image name will have a different process id and hence terminating a process by its process id will close only that process.

Example: To kill notepad application using the process id (here we assume the process id for the opened notepad application is 5452)5452
taskkill -PID 5452

To terminate all the opened notepad applications using image name
taskkill –IM “notepad.exe”

We can specify whether to forcefully terminate by using the parameter –F
taskkill –F –IM “notepad.exe”


2) net start  and net stop

The net start command is used to start a service using the command prompt.
The net stop command is used to stop a service from the command prompt.
Here one important thing to note is net is a command and start or stop are parameters / options

Syntax:
net start “service_name”
net stop “service_name”

Example: Starting and stopping SQL Services
net start MSSQLSERVER
net stop MSSQLSERVER

Tip: Using just net start in the command prompt we can see all the services that are started.
Similarly using net stop we can see all the services that are stopped.


3) appcmd start sites and appcmd stop sites

The appcmd start sites command is used to start particular site in IIS.
The appcmd stop sites command is ued to stop particular site in IIS.

Note – For the above commands to work the system path should be set to the directory “Main_Drive:\Windows\System32\inetsrv”
Here, replace Main_Drive with the letter of the drive where windows is installed, usually it will be C .

Syntax:
appcmd start sites “site_name”
appcmd stop sites “site_name”

Example: Starting and stopping the web site SampleWebSite
appcmd start sites “SampleWebSite”
appcmd stop sites SampleWebSite”


4) hostname

This command is use to get the name of the computer (also called as the hostname).

Syntax: hostname

Example: hostname
Tianhe-2

here, Tianhe-2 is the computer name.

or we can alternatively use the command  systeminfo | findstr -B -C:"Host Name" to get the computer name



5) shutdown

The shutdown command can be used to perform various system tasks like shutdown, restart , log off , hibernate etc.

Syntax: shutdown

Note – Let’s assume “testmachine” is the name of the computer for the below examples ,
when executing in your machine type the correct name of your machine, we can get the name of the machine using the command hostname as explained in earlier post

To restart a computer we can use the –r parameter as shutdown \\computer_name –r    
shutdown \\testmachine –r

To Log Off a computer we can use the –l parameter like  shutdown \\computer_name -l
Shutdown \\testmachine –l

cmd - shutdown -i
The above command will display a pop window where we need to add the computer to shutdown/Restart/LogOff
We have to select the option in the pop up window to
shutdown/Restart/LogOff


Further Reading -