Sunday, August 18, 2013

Windows Commands

   Over the recent years there are many new commands introduced in windows operating systems besides the original DOS commands. These newly added commands enable us to carry out operations which are quite helpful and sophisticated. The full documentations of all the commands is available on microsoft's msdn website.

    Following MS-DOS command copies files and directories from source to destination and "/E" creates empty directories, "/C" continues even if there is an error, "/H" includes hidden / system files, "/R" overwrites read only files in the destination, "/K" retaining the file attributes, and "/O" ownership / Access control list information, "/Y" avoiding prompting while overwriting the files.

    xcopy source destination /E /C /H /R /K /O /Y

   Following command copies files and directories from source to destination and  "/C" continuing even if there is an error, "/D" copy the file modified dates, "/S" copy files and subdirectories recursively except empty directories, "/H" include hiddern / system files

    xcopy source destination /C /D /S /H

       Robocopy is the very powerful external command to copy files in windows. Following command copies all the files including the empty directories from the given source location to destination,  

    robocopy source destination   /MIR

     It is used to kill one or more tasks / processes using process id or process name. The following command terminates the process by name forcefully.
    taskkill /im processname /f

     The following command on the other hand terminates all the processes running by the use name "john".
     taskkill /F /FI "USERNAME eq john"

     It displays active TCP connections, ports on which the computer is listening, Ethernet statistics, the IP routing table, IPv4 statistics. The following command displays the process actual file name using the "-b" option.

    netstat -b

     The remote shutdown tool enables to shutdown the local or remote computer within the network.

     Following command shuts down the computer by closing all the applications after specified time delay using "/t" option and displaying the message.
     shutdown \\computername /l /a /r /t:xx "msg" /y /c
     shutdown /l /t:120 "The computer is shutting down" /y /c

     Following command reboots "/r" the remote machine specified using "/m" option. It forces all the applications to close after a a minute delay "/t" with the reason "Application: Maintenance (Planned)" and the comment "/c" "Reconfiguring Applications" type:

     shutdown /r /m \\RemoteMachine /t 60 /c "Reconfiguring Applications" /f /d p:4:1

     Schtasks command is used to query or execute the tasks inside the Task Scheduler.

     Following command lists all the tasks present on the remote machine.
     schtasks /query /s \\RemoteMachine

     Following command lists all the tasks matching the name "MyTask" present on the remote machine.
     schtasks /query /s \\RemoteMachine  | findstr "MyTask"

     Following command runs the specified task name with the full path present on the specified remote machine.
     schtasks /run /s \\RemoteMachine /tn "\Microsoft\Windows\Tasks\MyTask"

     Similarly following command ends the specified task on the remote machine.
     schtasks /end /s \\RemoteMachine /tn "\Microsoft\Windows\Tasks\MyTask"

     Following command queries the task matching the name "\Microsoft\Windows\Tasks\MyTask" present on the remote machine. It displays advance properites of the task in a list format.
     schtasks /query /s \\RemoteMachine /tn "\Microsoft\Windows\Tasks\MyTask" /fo LIST /v

     Also we can create a new task in the task scheduler using the following command:
     schtasks /create /tn task_name       /tr "...\path\task.bat"       /sc daily              /st 10:00:00       /s \\ComputerName       /u username       /p password

7) SC:
     The SC command is used to communicate the service controller to manage windows services. It helps to create, update and delete windows service using various options which run as background processes. Note that all the sc command options require a space between the equals sign and the value.

     Following command creates a new window service with the specified name and run the executable specified along with the binpath option.
     sc create "servicename" binpath= "C:\Windows\System32\sample.exe" DisplayName= "Sample Service" start= auto

     Following command delete the windows service with the specified name.
     sc delete servicename

     Below command lists all the windows services on the command line.
     sc queryex type= service state= all | find "_NAME"

     Alternatively following service commands can be used to start/stop windows services:
     Start a service:       net startservice
     Stop a service:       net stopservice
     Pause a service:     net pauseservice
     Resume a service:  net continueservice

8) WMIC:
      The WMIC command provides a command line interface to Windows Management Instrumentation (WMI). WMI is the infrastructure to handle data and operations of the windows operating system and enables to carry out administrative tasks using WMI scripts.
     Following command gives the hardware architecture details of the CPU of the current machine
     wmic cpu get caption

     Below command provides the information regarding the current Windows OS architecture, primarily 32/64 bit system.
     wmic OS get OSArchitecture

     This is a utility tool which allows us to execute commands on the remote machines redirecting the remote console output to our local system. There are many other advance usages of the tool.

     psexec \\ComputerName cmd

      The NET USE command enables to connect or disconnect a computer computer from a shared resource, or to display information about computer connections. The below command assigns the disk drive Z: to the shared directory on \\zdshare

     net use Z: \\zdshare\IT\deploy

     The below command disconnects the Z drive from the \\zdshare directory.

     net use Z: /delete

     Help Option: Use the "/?" option to display the help for the command

     net use /?

      The FINDSTR command is used to search for patterns of text in files using regular expressions. Find the specified text "APC" with /c as a literal search string with non case-sensitive search. Also repeat the search for zero or more occurrences of previous character or class.

     findstr /i /c:"APC" *

No comments: