How to create and run a PowerShell script file on Windows 10
In this article, TipsMake will guide you how to create and run a PowerShell script file on Windows 10.
PowerShell is a command line tool designed by Microsoft to run commands and scripts to change settings or automate tasks. It sounds like Command Prompt. However, PowerShell is a more functional command line interface (CLI) with a variety of toolkits and flexible control and control capabilities.
While Command Prompt is only available on Windows, PowerShell is available on Windows, macOS, and Linux.
To create and run a PowerShell script file on Windows 10, follow these steps:
How to create a PowerShell script file
On Windows 10, you can virtually create a PowerShell script file using any text editor or ISE (Integrated Scripting Environment). However, it's best to use the Visual Sturio Code editor with the PowerShell extension.
Visual Studio Code - also known as VS Code - is a free and extensible cross-platform code editor. With VS Code, we can compose and edit almost any kind of programming language. And when you add the PowerShell extension, you'll have a fully interactive scripting experience.
So, the first thing you need to do is download and install VS Code:
- Visit Microsoft's VS Code download page here.
- Click the Windows button to download the installer
- Double click the left mouse button on the downloaded file to run the VS Code installer
- Agree to the terms and conditions
- Click Next
- Click Next again
- Click Next for the third time
- Confirm additional actions
- Click Next
- Click Install
- Wait for the loader to finish and click Finish
After downloading and installing VS Code, you need to install the PowerShell extension. Here's how to do it:
- Open VS Code
- Click the Extensions tab on the left side or click Ctrl + Shift + X
- Search for PowerShell and click on the first result
- Click the Install button
When you're done, you can start writing the PowerShell script file using VS Code:
- Open VS Code
- Click the File menu and choose New File
- Click the File menu and choose Save as
- In the File name field , name the file with the .ps1 extension , for example make tech easier.
- Click Save
You can write or paste the script you want to run, for example:
Write-Host "Welcome tipsmake script"
The script above when running will display the following text below the screen:
Welcome tipsmake script
You can run the script by pressing the Run button on the upper right side or pressing the F5 shortcut . After writing the script you should press the File menu and choose Save to save.
After you have finished, your script should be able to run but not execute your commands. This is because by default PowerShell is always set to block any script action. The only exception is when you run the script in VS Code or PowerShell ISE.
How to run a PowerShell script file on Windows 10
To run a script file with PowerShell you need to change the execution policy on Windows 10.
On Windows 10, PowerShell includes 4 enforcement policies including:
- Restricted - Do not allow any scripts to run
- RemoteSigned - Allows you to run scripts created on the device but block scripts created on other computers unless they are signed by a trusted entity.
- AllSigned - All scripts will run when they are signed by a trusted entity
- Unrestricted - Run any script on the computer, without any restrictions
Here's how you set up to run PowerShell scripts on Windows 10:
- Press Windows to open the Start menu
- Type
powershell
then right-click on the search results to run PowerShell as Administrator - In the PowerShell window , enter the following command and then press Enter :
Set-ExecutionPolicy RemoteSigned
- Type A and press Enter
- Next, you use the following command to run the script. Remember to change the path and filename of the script file to suit your needs:
& "C:/PATHTOSCRIPT/tipsmake_script.ps1"
Above is how to create and run PowerShell script files on Windows 10. Wish you success and invite you to read more great tips on our site.
You should read it
- How to install PowerShell 7.0 in Windows 10/8/7
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 5
- How are Command Prompt (cmd) and PowerShell different?
- 10 PowerShell commands help you manage the remote computer most effectively
- About PowerShell
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 3
- Test SQL Server with Windows PowerShell - Part 1
- Use PowerShell to download any file on Windows 10
May be interested
- 10 tips with PowerShell in Windows Server 2008 - Part 1in fact, there are a lot of windows server 2008 tasks that we can do a lot faster with powershell than the gui-based application or tool. in the following article, we will introduce you some basic and most frequently used operations with powershell ...
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 11part 10 of this article series showed how to use powershell scripts in conjunction with smo and parameters to create sql server scripts. in this section, we will show you how to use powershell cmdlets in conjunction with the sql server client and output saving to export to a text file or xml file.
- About PowerShellwindows powershell is a command line utility and new scripting language provided by microsoft. why should i study and care about powershell? because it is a new-style utility? of course, every new utility is claimed to be 'different' from the old ones, but powershell has some components that really distinguish it from other utilities.
- What is Windows PowerShell? How to use Windows PowerShelldo you want to learn the features of windows powershell but don't know how? suggestions for you what is windows powershell? the most convenient way to use windows powershell.
- Instructions on how to use PowerShell in Windows Server 2012what is powershell? windows powershell is a command-line shell language interpreter and scripting language specifically designed for system administrators. built on the .net framework, windows powershell helps it professionals control and automate windows operating system administration tasks as well as applications running on windows server environments.
- What to do when Windows can't find PowerShell?powershell is a handy tool that allows you to automate tasks, fix various errors, and manage several windows installations. but what if it suddenly disappears from your computer?
- How to add 'Edit or Run with' to the right-click menu of PS1 filesa ps1 file is a script or cmdlet, used by windows powershell. this tutorial will show you how to add or remove a custom edit or run with menu to the context menu for powershell .ps1 files for all users in windows 10.
- 10 tips with PowerShell in Windows Server 2008 - Part 2in the previous article, i have shown you some of the basic functions and tricks that can be done with powershell in windows server 2008. and this time, we will continue with part 2, which is also the end. in powershell series in windows server environment ...
- Test SQL Server with Windows PowerShell - Part 7in this section, i will show you how to get that information in top 10 queries based on cpu performance.
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 9in part 9 of this series, i will show you how to use powershell in conjunction with smo to create a sql server script. creating sql server scripts is an important task for administrators and sql server database development professionals.