Run script from file windows

How to run .sh or Shell Script file in Windows 10

Shell Scripts or .SH files are like batch files of Windows which can be executed in Linux or Unix. It is possible to run .sh or Shell Script file in Windows 10 using Windows Subsystem for Linux. In this post, we will show you how to run a Shell Script file in Windows 10.

How to run .sh or Shell Script file in Windows 10

Bash is a Unix shell and command language which can run Shell Script files. You do not need to install Ubuntu or any other Linux Distros unless your scripts need the support of the real Linux kernel. We will share both the methods.

  1. Execute Shell Script file using WSL
  2. Execute Shell Script using Ubuntu on Windows 10

1] Execute Shell Script file using WSL

Install WSL or Windows Subsystem for Linux

Go to Settings > Update & Security > For Developers. Check the Developer Mode radio button. And search for “Windows Features”, choose “Turn Windows features on or off”.

Scroll to find WSL, check the box, and then install it. Once done, one has to reboot to finish installing the requested changes. Press Restart now. BASH will be available in the Command Prompt and PowerShell.

Execute Shell Script Files

  1. Open Command Prompt and navigate to the folder where the script file is available.
  2. Type Bash script-filename.sh and hit the enter key.
  3. It will execute the script, and depending on the file, you should see an output.

On a Linux platform, you usually use SH, but here you need to use BASH. That said, BASH in Windows has its limitations, so if you want to execute in a Linux environment, you need to install Ubuntu or anything similar.

2] Execute Shell Script using Ubuntu on Windows 10

Make sure you have Ubuntu or any other Linux distros installed. Ubuntu will mount or make all your Windows directories available under /mnt. So the C drive is available at /mnt/C. So if the desktop will be available at /mnt/c/users/ /desktop.

Now follow these steps

  1. Type Bash in run prompt, and it will launch the distro prompt.
  2. Navigate to the folder using “cd” command to the folder where the scripts are available.
  3. Type “sh script.sh” and hit enter.

It will execute the script, and if they have a dependency on any of core Linux features.

Since Linux is now available in Windows, you need not use any third party applications like Cygwin. WSL should be enough for most of the scenarios to help you run a shell script in Windows 10.

Date: July 15, 2019 Tags: WSL

Windows Subsystem for Linux has no installed distributions

Failed to Fork error with Ubuntu on Windows 10

WslRegisterDistribution failed with error 0x8007019e and 0x8000000d – WSL

[email protected]

Ashish is a veteran Windows, and Xbox user who excels in writing tips, tricks, and features on it to improve your day to day experience with your devices.

Running a shell script through Cygwin on Windows

I have a bunch of shell scripts that used to run on a Linux machine. Now, we’ve switched over to Windows, and I need to run these scripts there. I have Cygwin installed, but is there a way to make the script run using Cygwin, but the call is made from Windows batch?

Читайте также:  Есть установочная флешка с mac os x

6 Answers 6

Sure. On my (pretty vanilla) Cygwin setup, bash is in c:\cygwin\bin so I can run a bash script (say testit.sh ) from a Windows batch file using a command like:

. which can be included in a .bat file as easily as it can be typed at the command line, and with the same effect.

One more thing — if You edited the shell script in some Windows text editor, which produces the \r\n line-endings, cygwin’s bash wouldn’t accept those \r . Just run dos2unix testit.sh before executing the script:

If you have access to the Notepad++ editor on Windows there is a feature that allows you to easily get around this problem:

  1. Open the file that’s giving the error in Notepad++.
  2. Go under the «Edit» Menu and choose «EOL Conversion»
  3. There is an option there for «UNIX/OSX Format.» Choose that option.
  4. Re-save the file.

I did this and it solved my problems.

Hope this helps!

Just wanted to add that you can do this to apply dos2unix fix for all files under a directory, as it saved me heaps of time when we had to ‘fix’ a bunch of our scripts.

I’d do it as a comment to Roman’s answer, but I don’t have access to commenting yet.

The existing answers all seem to run this script in a DOS console window.

This may be acceptable, but for example means that colour codes (changing text colour) don’t work but instead get printed out as they are:

I found this solution some time ago, so I’m not sure whether mintty.exe is a standard Cygwin utility or whether you have to run the setup program to get it, but I run like this:

. this causes the script to run in a Cygwin BASH console instead of a Windows DOS console.

If you don’t mind always including .sh on the script file name, then you can keep the same script for Cygwin and Unix (Macbook).

To illustrate:
1. Always include .sh to your script file name, e.g., test1.sh
2. test1.sh looks like the following as an example:
#!/bin/bash echo ‘$0 = ‘ $0 echo ‘$1 = ‘ $1 filepath=$1 3. On Windows with Cygwin, you type «test1.sh» to run
4. On a Unix, you also type «test1.sh» to run

Note: On Windows, you need to use the file explorer to do following once:
1. Open the file explorer
2. Right-click on a file with .sh extension, like test1.sh
3. Open with. -> Select sh.exe
After this, your Windows 10 remembers to execute all .sh files with sh.exe.

Note: Using this method, you do not need to prepend your script file name with bash to run

How-to: Create and Run a CMD batch file

To prepare a new batch script, save the file as plain ASCII text with the file extension .CMD

Run a batch file

A batch file can be run by double clicking it in Windows explorer, or by typing the name/path at the command line, optionally passing any parameters needed.

From the start menu: START > RUN c:\path_to_scripts\my_script.cmd , OK

If the filename includes any spaces, then you will need to surround the command with quotes:

«c:\path to scripts\my script.cmd «

Open a new CMD prompt by choosing START > RUN cmd , OK

From the command line, enter the name of the script and press return.

C:\Batch> Demo.cmd
or
C:\Batch> c:\path_to_scripts\my_script.cmd param1 param2

This can be made easier by creating a shortcut for the start menu or taskbar.

To run a batch file from within another batch file, use the CALL command, otherwise the first script will start the second script and immediately exit, so any further commands in the first script will not run.

It is also possible to run batch scripts with the old (Windows 95 style) .BAT extension, but be aware that these will run in 16 bit compatibility mode, and that sets the ERRORLEVEL according to the old MSDOS rules.

View the startup command line

The environment Variable %CmdCmdLine% will expand into the original command line passed to CMD.EXE

When a batch file is launched from the command line %CmdCmdLine% will return:
C:\WINDOWS\system32\cmd.exe param1

When a batch file is launched by double clicking in Windows Explorer or START > RUN, %CMDCMDLINE% will return:
C:\WINDOWS\system32\cmd.exe /c «»C:\demo\batch.cmd param1

The /c can be used to detect the start mode:
Echo %CmdCmdLine% | findstr /c:» /c » >nul && Echo Started with a double click.

Читайте также:  Чем загружена оперативная память компьютера windows 10

Run a Powershell script

To run a PowerShell script from the CMD shell:

If the arguments need quotes you will need to triple them so they are escaped:

When calling PowerShell from CMD be aware that a comma is a CMD delimiter, this makes it impossible to pass an array of comma separated values to PowerShell. item1,item2,item3 is treated the same as item1 item2 item3

Run a VBScript file

To run a VBScript from the CMD shell:

“The method of the enterprising is to plan with audacity and execute with vigor”

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.
  • 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.
  • Читайте также:  Как узнать владельца файла windows

    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:

    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.

    Review: NZXT made its first AMD motherboard and it’s brilliant

    NZXT made its first motherboard with an AMD chipset. The N7 B550 supports the latest AMD Ryzen processors and there’s plenty to love about this platform. To differentiate the N7 B550, NZXT made notable changes to the layout of the motherboard to make it easy to create a clean PC build.

    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!

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