Using windows supported commands for automated tasks

cuongld2

cuongld2

Posted on November 15, 2019

Using windows supported commands for automated tasks

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

  • 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

  • You want to automate things. For example like auto start jenkins when the machine restarted
    Alt Text

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

Or kill a browser instance:

Alt Text

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

Alt Text

Or uninstall browser silently:

Alt Text

2.Files

If we want to delete an image file:

Alt Text

3.System

For example with whoami command:

Alt Text

4.Network
Legendary command would be ping:

Alt Text

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

  • Get-Service | Where-Object {$_.Status –eq “Running”}
    Alt Text

  • Get-Service | Where-Object {$_.Status -eq “Running”} | Select-Object Name
    Alt Text

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:

@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.
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
cuongld2
cuongld2

Posted on November 15, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related