How to make scripts in windows

How to Write a CMD Script

If you have every used the Command Line, or CMD, interface in Windows, you probably have some idea of the powerful things it can do. Creating your own CMD scripts, you can do even more, but faster.

A CMD script does the same thing as if you typed commands into the CMD window. If you want to do something on a regular basis, such as telling Windows to turn off your computer after an hour, you can write a script and then you can activate the script whenever you want to run it.

Understanding CMD and Written Commands

In the early days of personal computing almost everything was done by typing command_s into a command line interface. If you wanted to open a program, you had to type the name of the program into the command line. Today, you can simply click or touch an icon on your screen to perform most actions. But Windows still accepts type-written commands in the CMD utility. You can write commands_ to open programs, add or change account permissions, back up files or get information about your computer using the CMD window.

Understanding CMD Scripts

The Command Prompt utility in Windows can be opened at any time, simply by typing «cmd» in the Windows Start menu. Here, you can type all sorts of commands to open programs, change settings and make tweaks to how Windows and its programs perform. In Microsoft’s long history of operating systems, CMD i_s a relative newcomer. In MS-DOS, before Windows was released, when you wanted to run a script, you would save it as a .bat file. While you can still save files with that extension today, most people use the .cmd extension._

Using a Script CMD to Open Notepad

To create and save a CMD switch, it’s best to use a basic text editor. Using a word processor like Microsoft Word makes saving the file a hassle. Notepad is much easier to use. So to demonstrate how CMD works, let’s open use it to open Notepad.

  1. Type CMD in the Windows Start menu and press Enter to open CMD.exe.
  2. Change the directory from your current username folder to the base directory by typing «cd\» and pressing Enter. It should now read «C:\>» before the blinking cursor.
  3. Type the following line and pressEnter:start «c:\windows\system32» notepad.exe

As soon as you press Enter, you will see Notepad open. The command you entered has told Windows to start the notepad.exe program, which is located in the system32 folder, which is inside the Windows folder, on the C: drive. CMD commands are not case-sensitive so you can use lowercase or uppercase letters interchangeably.

Creating Your First Script CMD File

Now that Notepad is open, create your first CMD script file by typing the same line you used in the CMD window into notepad: start «c:\windows\system32» notepad.exe

Save the batch file to your desktop by selecting «Save As» from the File menu. Name the file «firstscript.cmd» and click «Save.» Notepad script commands must be saved with the .cmd extension, rather than the default .txt extension.

Double-click the new CMD file on your desktop. You will see the CMD window open for a fraction of a second and then close as Notepad is launched.

This is hardly a useful script, since a desktop shortcut does the same thing. To create something more useful, let’s edit the file so that it creates a new text file on your desktop listing all of your programs.

Using Echo and Echo Off

While the CMD window wasn’t open long enough to see it, by default it will always display the text that was entered in the CMD file when it runs. For longer scripts, this can be a nuisance, so it’s generally good form to turn this off by using the Echo Off command in the first line of the CMD file. By itself, Echo Off disables the display of any text that follows it. To make the Echo Off command apply to itself, put an @ symbol in front of it. Thus, your two-line CMD script would be:

Читайте также:  Android sdk linux package

@echo off

start «c:\windows\system32» notepad.exe

Creating a Text File Script

This CMD script will list all the files you have in your Program Files folder and put that list in a new text file.

  1. Open Notepad. Type «@echo off«in the first line and press Enter.
  2. In the second line, type: dir «C:\Program Files» > list_of_files.txt
  3. Select «Save As» from the File menu and save the file as «program-list-script.cmd.»
  4. Double-click the new text file on your desktop to see the list of files and folders.

The text file will appear in the same folder where the script file itself is. So if the script file is on your desktop, the list-of-files.txt file will also appear on your desktop.

If you want to change the folder where the text file is placed, you can specify its own folder in the script. For example, if you want it to be placed in your Documents folder, use: dir «C:\Program Files» > C:\Users\David\Documents\list_of_files.txt

Creating a Systems Information Script

If you want to use a script to give you needed information, it’s not always necessary to produce a text document with a script. You can have the information posted directly in the CMD window.

The example script below will give you basic information about your computer, including the operating system and version number, the BIOS version, the total physical memory and your computer’s network address. To use the script, type or copy the lines below into a new Notepad file and save it with the .cmd file extension, such as «my_computer_info.cmd.»

In this example, ECHO OFF is used to prevent the CMD window from displaying the script.

The ECHO command is used to display specific text, as well as some equal signs (===) as lines to organize the information in sections.

To insert a comment for your own use — without it affecting the script or appearing in the CMD window — type two colons first. Anything in the same line following » :: « will be commented out from the script.

The PAUSE command directs the CMD program to stay open. Pressing any key on your keyboard will close the window.

@ECHO OFF

:: This CMD script provides you with your operating system, hardware and network information.

TITLE My System Info

ECHO Please wait. Gathering system information.

ECHO OPERATING SYSTEM

systeminfo | findstr /c:»OS Name»

systeminfo | findstr /c:»OS Version»

ECHO BIOS

systeminfo | findstr /c:»System Type»

ECHO MEMORY

systeminfo | findstr /c:»Total Physical Memory»

ECHO CPU

wmic cpu get name

ECHO NETWORK ADDRESS

ipconfig | findstr IPv4

ipconfig | findstr IPv6

PAUSE

Using Scripts to Shut Down Your Computer

Normally, when you shut down your computer, it happens instantaneously. Suppose, however, that you’re listening to an audiobook or watching a training video — and you know that you will fall asleep after an hour. You can use a CMD script to tell your computer to shut itself down, after a specified period of time, using the shutdown command.

When you use the shutdown command, you need to include two additional switches, or subcommands. The first tells the computer to either shutdown or restart. You can use either -s or — r. The second tells the computer how many seconds to wait before performing the command. For this you use -t, followed by the number of seconds.

To shutdown the computer in one second, use: shutdown -s -t 01

To restart the computer in eight seconds, use: shutdown -r -t 08

To shutdown the computer in two hours use: shutdown -s -t 7200

Backing Up Files With a CMD Script

If you find it tedious to back up your files to a second storage device, using a CMD script makes the process a breeze. For this, use the Robocopy command. For example, if you want to back up all of the files in your Documents folder onto a removable storage device, you can write the command in a CMD file and then — at the end of the day — simply double click the file to activate it.

The Robocopy command needs to know, first — which folder you want to copy and, second — where you want the copy placed. Both the source and destination need to be in quotation marks.

Читайте также:  Smart notebook для windows

If you’re not certain what your drive letters are, open File Explorer and click on «My Computer.»

For example, if your User name is MyName, your Documents folder is in your C: drive and your Backup folder is in a removable storage D: drive, then the command would be:

robocopy D:\Users\MyName\Documents F:\Backup /XA:H /W:0 /R:1 > F:\Backup\backup.log

This example is a bit more complicated, since Robocopy offers you a lot of options.

D:\Users\MyName\Documents: the folder you want to back up.

F:\Backup: the location of your Backup folder.

/XA:H: ignores hidden files.

/W:0: waits zero seconds between retries, instead of the default 30 seconds.

/R:1: retry only once if the file is locked.

> F:\Backup\backup.log: create a backup log placed in the Backup folder.

Note that because this is a mirror backup, if you delete files from the source folder, they will be deleted from the Backup folder the next time you use the script. It would be a good idea to explore additional switches available for Robocopy, so that you ensure that you backup your files the way that works best for you.

How to create and run a PowerShell script file on Windows 10

Source: Windows Central

On Windows 10, PowerShell is a command-line tool designed by Microsoft to run commands and scripts to change settings and automate tasks. In a way, it’s similar to Command Prompt. However, PowerShell is a more capable command-line interface (CLI) that offers an extensive set of tools and more flexibility and control. Also, unlike Command Prompt, PowerShell is available on Windows, macOS, and Linux.

A script is just a collection of commands saved into a text file (using the special «.ps1» extension) that PowerShell understands and executes in sequence to perform different actions.

The only caveat is that the default security protocol always blocks any script from running on a device. This means that when double-clicking a «.ps1» file on Windows 10 nothing will happen, and if you try to run the script within PowerShell, you’ll see the «cannot be loaded because running scripts is disabled on this system» error message. However, it’s not impossible to run scripts on your computer. You only need to enable the correct execution policy.

In this Windows 10 guide, we’ll walk you through the steps to successfully write and run your first script file on PowerShell using Visual Studio Code, Notepad, and the PowerShell Integrated Scripting Environment (ISE) console.

How to create PowerShell script file on Windows 10

On Windows 10, you can create PowerShell script files using virtually any text editor or the ISE console. However, the preferred option (thanks @jotaka for the heads up) to build scripts moving forward is to use the Visual Studio Code editor with the PowerShell extension.

Creating script with Visual Studio Code

Visual Studio Code — also known as VS Code — is a free and extensible cross-platform code editor that provides an environment to edit virtually any kind of programming language. And when adding the PowerShell extension, you get a fully interactive scripting editing experience, even with IntelliSense (code-completion) support.

The new experience is meant to be the new default, but the PowerShell ISE console isn’t going away. Still, the company won’t be adding any more features, and it doesn’t support PowerShell 7 or higher releases.

Install Visual Studio Code

To install Visual Basic Code on Windows 10, use these steps:

Click the Windows button to download the installer.

Source: Windows Central

Click the Next button.

Source: Windows Central

Confirm additional tasks as necessary.

Source: Windows Central

  • Click the Next button.
  • Click the Install button.
  • Click the Finish button.
  • Once you complete the steps, you can proceed to install the PowerShell extension.

    Install PowerShell extension

    To install the PowerShell extension on VS Code, use these steps:

    1. Open VS Code.
    2. Click the Extensions tab from the left pane.
    3. Search for PowerShell and select the top result.

    Click the Install button.

    Source: Windows Central

    After you complete the steps, you can start writing PowerShell scripts using Visual Studio Code.

    Create PowerShell script with Visual Studio Code

    To create a script with Visual Basic Code, use these steps:

      Open VS Code.

    Click the File menu and select the New File option.

    Source: Windows Central

    Click the File menu and select the Save as option.

    Source: Windows Central

    Write a new, or paste the script you want to run — for example:

    Write-Host «Congratulations! Your first script executed successfully»

    The above script will output the phrase «Congratulations! Your first script executed successfully» on the screen.

    (Optional) Click the Run button from the top-right side (or press the F5 key) to run the script.

    Source: Windows Central

  • Click the File menu.
  • Click the Save option.
  • Читайте также:  Windows control panel ports

    Creating script with Notepad

    To create a PowerShell script using the Notepad editor on Windows 10, use these steps:

    1. Open Start.
    2. Search for Notepad, and click the top result to open the app.

    Write a new, or paste your script, in the text file — for example:

    Write-Host «Congratulations! Your first script executed successfully»

    Source: Windows Central

    Type a descriptive name for the script — for example, first_script.ps1.

    Source: Windows Central

  • Click the Save button.
  • Creating script with Integrated Scripting Environment

    Alternatively, you can use the built-in PowerShell ISE console to code your scripts on Windows 10.

    The Integrated Scripting Environment is an advanced tool, but you can get started using these steps:

    1. Open Start.
    2. Search for Windows PowerShell ISE, right-click the top result, and select the Run as administrator option.
    3. Click on File menu.

    Select the New option to create a new empty .ps1 file.

    Source: Windows Central

    Write a new, or paste the script you want to run — for example:

    Write-Host «Congratulations! Your first script executed successfully»

    Source: Windows Central

    Type a name for the script – for example, first_script.ps1.

    Source: Windows Central

  • Select the folder location to store the script.
  • Click the Save button.
  • Once you complete the steps using Notepad, Visual Studio Code, or PowerShell ISE, the script will be ready to run, but it will fail by default. This is because the default PowerShell settings are always set to block the execution of any script. (The only exception is if you run the contents of the script within Visual Studio Code or PowerShell ISE.)

    How to run PowerShell script file on Windows 10

    If you wish to run a script file with PowerShell, you have to change the execution policy on Windows 10.

    To change the execution policy to run PowerShell scripts, use these steps:

    1. Open Start.
    2. Search for PowerShell, right-click the top result, and select the Run as administrator option.

    Type the following command to allow scripts to run and press Enter:

    Type A and press Enter (if applicable).

    Source: Windows Central

    Type the following command to run the script and press Enter:

    In the above command, make sure to change «PATH\TO\SCRIPT» to the location of your script.

    For example, this command runs a script stored in the Downloads folder:

    Source: Windows Central

    After you complete the steps, the script will run, and if it was written correctly, you should see its output without issues.

    On Windows 10, PowerShell includes four execution policies, including:

    • Restricted — Stops any script from running.
    • RemoteSigned — Allows scripts created on the device, but scripts created on another computer won’t run unless they include a trusted publisher’s signature.
    • AllSigned — All the scripts will run, but only if a trusted publisher has signed them.
    • Unrestricted — Runs any script without any restrictions.

    In the above steps, we use the command to allow local scripts to run on Windows 10. However, if you’re not planning to run scripts regularly, you can restore the default settings to block untrusted scripts using the same instructions outlined above, but on step No. 4, make sure to use the Set-ExecutionPolicy Restricted command.

    More Windows 10 resources

    For more helpful articles, coverage, and answers to common questions about Windows 10, visit the following resources:

    Halo: MCC’s live service elements make it better, not worse

    Halo: The Master Chief Collection is more popular than ever, but some fans don’t agree with the live service approach 343 Industries has taken with it. Here’s why those elements are, at the end of the day, great for the game and for Halo overall.

    Microsoft’s Surface Duo is not ‘failing up’

    Microsoft announced this week that it was expanding Surface Duo availability to nine new commercial markets. While Surface Duo is undoubtedly a work in progress, this is not a sign of a disaster. It’s also doesn’t mean that Surface Duo is selling a ton either. Instead, the reason for the expansion is a lot more straightforward.

    Here’s what you can do if Windows 10 update KB5001330 is causing issues

    In this guide, we’ll show you the steps to get rid of the update KB5001330 to fix profile, gaming, and BSoD problems with the Windows 10 October 2020 Update and May 2020 Update.

    These are the best PC sticks when you’re on the move

    Instant computer — just add a screen. That’s the general idea behind the ultra-portable PC, but it can be hard to know which one you want. Relax, we have you covered!

    Оцените статью