Using windows supported commands for automated tasks
Tags: windows, commandprompt, powershell, automation
Published: 2019-11-15 10:25:10

---
title: Using windows supported commands for automated tasks
published: true
description: This post aims to provide general ideas for utilize windows supported commands to do automation tasks
tags: #windows #command-prompt #power-shell #automation
cover_image: https://thepracticaldev.s3.amazonaws.com/i/larcbhudjjuo9llntqly.png
---

I.Why we use command line
Doing tasks via GUI will be easier and for everyone use. So why we would want to use command line?

We use command line to talk to the computer, not by action in GUI.

We will need to use command line for the below scenarios:

   - You need to remote access to other machines which might not have GUI
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/irdlbhz961xu0qw9y8uq.png)

   - You need to do repetitive tasks which almost impossible to do in GUI manually. For example renaming more than 1000 files in a directory.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/yullf90c2h07xbgbndi6.jpg)

   - You want to automate things. For example like auto start jenkins when the machine restarted
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4h7oftvfq0emrio3mp3u.png)

II.Command prompt

Command prompt also know as 'cmd.exe' support only windows native command line.

For example you cannot use 'ls' command in this (ls is for listing file in Unix like system)

There are almost 300 commands for Windows and its predecessor, MS-DOS. Over the decades, some commands have been kept around, some have only recently been added with newer versions of Windows, and others have been removed or replaced. In our comprehensive list, we explain what the different commands mean and on which Windows versions they run. This way, you can quickly look up whether the CMD commands that you know still function with Windows 10. To make it more clear, we’ve divided the Windows command prompt commands into four categories: basics, files, system, and network.
Below I will show some example for each category.
1.Basics

If we want to show the current version number of windows:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/j4kbkg2x34ezm7c4vfj6.PNG)

Or kill a browser instance:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/1t3stb8oscv4xs5s5n9e.PNG)

Or install browser silently (this command used to install CocCoc browser : https://coccoc.com/en/)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/wsk2j7to5p5dcklyz619.PNG)

Or uninstall browser silently:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/7mjl97ic1pf96h6chpy5.PNG)

2.Files

If we want to delete an image file:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/klwlmdh0cpjerkrznrnj.PNG)

3.System

For example with whoami command:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/idyfongof6tnn3reqyh5.PNG)

4.Network
Legendary command would be ping:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/am86ubm1n7rh3wae1qpa.PNG)

III.Powershell

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

PowerShell commands let you manage computers from the command line. PowerShell providers let you access data stores, such as the registry and certificate store, as easily as you access the file system. PowerShell includes a rich expression parser and a fully developed scripting language.

Powershell supports not only native windows command but also linux commands.

Powershell is very different from command prompt. It uses different commands, known as cmdlets in PowerShell. Many system administration tasks — from managing the registry to WMI (Windows Management Instrumentation) — are exposed via PowerShell cmdlets, while they aren’t accessible from the Command Prompt.

PowerShell makes use of pipes—just as Linux does—that allow you to pass the output of one cmdlet to the input of another cmdlet. Thus, you can use multiple cmdlets in sequence to manipulate the same data. Unlike Unix-like systems—which can only pipe streams of characters (text)—PowerShell pipes objects between cmdlets. And pretty much everything in PowerShell is an object, including every response you get from a cmdlet. This allows PowerShell to share more complex data between cmdlets, operating more like a programming language.

PowerShell isn’t just a shell. It’s a powerful scripting environment you can use to create complex scripts for managing Windows systems much more easily than you could with the Command Prompt.

The Command Prompt is essentially just a legacy environment carried forward in Windows—an environment that copies all of the various DOS commands you would find on a DOS system. It is painfully limited, can’t access many Windows system administration features, is more difficult to compose complex scripts with, and so on. PowerShell is a new environment for Windows system administrators that allows them to use a more modern command-line environment to manage Windows.

Some example commands in powershell:

- Get-Service
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0p4ms5zh2yb34ah4v9zv.PNG)

- Get-Service | Where-Object {$_.Status –eq “Running”}
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nw0hpv2rkq5mrgdhn1bq.PNG)

- Get-Service | Where-Object {$_.Status -eq “Running”} | Select-Object Name
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/dd2xvwauxzn0ka5peqqf.PNG)

IV.Stored commands in file for automate task:
1.Batch file:
Normally you should store native windows command in file .bat.
You can call this file as when the windows restart, or schedule to run it at particular time

Batch file could look like this:
````javascript
@echo off

IF NOT EXIST %WINDIR%\System32\GroupPolicy goto next

echo Deleting GroupPolicy folder...
RD /S /Q "%WINDIR%\System32\GroupPolicy" || goto error
echo.

:next
IF NOT EXIST %WINDIR%\System32\GroupPolicyUsers goto next2

echo Deleting GroupPolicyUsers folder...
RD /S /Q "%WINDIR%\System32\GroupPolicyUsers" || goto error
echo.

:next2
gpupdate /force

pause
exit

:error
echo.
echo An unexpected error has occurred. ¨Have opened the program as an administrator (right click, run as administrator)?
echo.
pause
exit
````
Above is the batch file to delete chrome policies.
@echo off to prevent print to log what command will be used

2.Powershell commands file:
Power script file has extension is '.ps1'

To automatically run the '.ps1' file, you can create '.bat' or '.cmd' file, and from that file, you run the powershell file.

For more details, you can refer to this guide : [powershell-script-autorun](https://www.addictivetips.com/windows-tips/auto-run-powershell-scripts-windows-10/)

V.Code editor support powershell and bat file

I would suggest to use visual studio code.
You can download from here: [visual-studio-code](https://code.visualstudio.com/)

1.Bat file
Currently I don't see the extension support for batch script.
But when you save the bat file in VS code, it will hightlight the correct command , so maybe it good for you to write the script.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/w9hmma7qdj7pan6b54cm.PNG)

2.Powershell file

You can install extension for powershell script.
Currently I'm using ms-vscode.powershell

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/sa5be6fsgh0dhq733ka5.PNG)

The below is what '.ps1' file looks like in vs-code
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/732vkeebt6t5bivd82s5.PNG)

As you write powershell script in vs-code, there will be autocomplete suggest for you.

That's it. It's been a long time I did not write any blog. So hopefully, after this, I can write more.
Peace!!!!

Notes: If you feel this blog help you and want to show the appreciation, feel free to drop by :

[<img src="https://thepracticaldev.s3.amazonaws.com/i/cno42wb8aik6o9ek1f89.png">](https://www.buymeacoffee.com/dOaeSPv
)

This will help me to contributing more valued contents.