How to Create and Run PowerShell Scripts: A Step-by-Step Guide (2023)

Working with PowerShell scripting can be an exciting possibility. After all, it provides a clean interface for creating scripts to automate everyday tasks.

If you're a developer, you can build complete automation scripts - PowerShell is great at this, especially if you're using it to build, test, and deploy solutions for CI/CD environments.

In this article, we'll take a closer look at creating and running PowerShell scripts. Here we discuss a step by step guide on how to do itCode VSNotepad and integrated scripting environment.

Let's begin.

What is PowerShell?

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (1)

PowerShellis a cross-platform automation and configuration tool. It provides a complete suite for administrators, developers and other users to use cmdlets (small and lightweight command) to manage various tasks.

As part of the package you get:

  • Commandoregel-shell
  • associatethe language of letters
  • Framework for handling cmdlets

Unlike the command line, it is available on all major platforms including Windows, Linux and macOS.

Technically speaking, PowerShell is based on .NET. Its approach supports objects and structured data such as JSON, CSV and XML. Developers can also use REST APIs with PowerShell. Unfortunately, the basic command line doesn't support any of that because it's text-based.

All PowerShell scripts are written in .ps1expansion.

When writing scripts in PowerShell, you need to know three key concepts:

  • cmdlet:They are atomic executable scripts that provide pre-configured functions. For example, you can use Copy-Item to copy, Get-Help to get help, or Write-Host to display something on the screen.
  • Shortcuts:Aliases provide an easy way to refer to cmdlets. Provides faster access. However, it is recommended to use them with queries for interactive purposes and not when writing PowerShell scripts.
  • Features:A function is a set of PowerShell statements. Users can invoke a function by calling.

PowerShell and scripts

Scripts are the primary method of automating aspects of your work. Whether you're a developer, administrator, or freelancer, scripts can automate everyday work processes

After all, scripts have many advantages, including:

  • Save valuable time with automation.
  • Transferable knowledge.
  • Ensure consistency between systems.
  • It improves your skills and employability.

You can run PowerShell, Bash Scripting, Ruby or Python scripts.

In general, PowerShell is a great scripting tool. However, Windows prevents scripts from running due to security settings. This can lead to errors like "Scripting is disabled on this system". To work around it, you need to set the correct execution policy. (discussed later in the article).

Why use PowerShell for scripting?

One of the biggest reasons why PowerShell is a great choice for scripting is its cross-platform ability. It supports Windows, Linux and macOS that the standard Windows command line does not support.

You can use PowerShell to create simple tasks with its simple task builder tools. For example, you can use PowerShell to restart your computer, periodically clear your cache, and even check your session.

In addition, PowerShell scripts are mainly used to automate system administration. However, you can use it as a developer to test, create and deploy upgrades through a CI/CD environment.

How are PowerShell scripts created?

You need a text editor to create PowerShell scripts. You can usually do just fine with a simple text editor, but a modern text/code editor makes the whole process much more manageable.

So, first we'll show you how to create PowerShell scripts using Visual Studio Code (also known as VS Code). We also discuss how to create PowerShell scripts in Notepad and other integrated scripting environments, such as the PowerShell Integrated Scripting Environment. (PowerShell ISE).

# 1.With VS code

VS Code is a robust and reliable free cross-platform code editor. It is developed by Microsoft with the added benefit of good PowerShell support.

You get a clean interface right out of the box and you can use it with almost anyoneprogramming language.

Since VS Code is a good code editor, it supports extensions. To enhance our PowerShell scripting environment, we use the PowerShell extension.

Install the Visual Studio code

But for that you need to install VS Code.

Go to for thisVisual Studio Code Download Page.Select the appropriate version of VS Code based on your operating system. Since we are using Windows 11, we decided to go with the Windows version.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (2)

After downloading, double-click the installer and follow the wizard to install it on your computer.

Install the PowerShell extension

You can install the PowerShell extension in two ways. First, create a new file with the extension.ps1expansion.

VSCode is smart enough to know you're working with PowerShell and automatically recommends installing the "PowerShell Extension" from Microsoft.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (3)

You can click "Install" to install it. Otherwise, click "View recommendation" for more information.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (4)

If for some reason Visual Studio Code didn't give you recommendations, you can search for the plugin manually.

Click on the symbol of four squares (with onesquaredeparture) or pressCtrl+Shift+Xto open an extension window. Now search for "Microsoft PowerShell" and click on the first result. From there, go to Install.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (5)

Finally, you will be asked if you want to trust the authors of the files in your workspace. Click "Trust Workspace & Install" to complete the installation.

Create a PowerShell script with VS Code

You can now create your first Powershell script with VS Code and its extensions properly installed.

If you have not created a new file, you can do so by following the steps below.

  1. Open VS Code
  2. Go to the top menu, click File, then New Text File.
  3. Now save the file by going to File > Save As.
  4. Enter the desired file name and do not forget to add the extension .ps1
  5. Finally, click Save File.
How to Create and Run PowerShell Scripts: A Step-by-Step Guide (6)

That's it! Now you can build your script accordingly. I added a Write-Host cmdlet with the value "This is a VS Code script written for Geekflare".

We'll see how to run the script later, so keep reading!

#2.With a notebook

Creating PowerShell scripts in Notepad is easy.

All you have to do is follow the steps below:

1. Open Start and search for Notepad.

2. Now right click on it and select "Open as Administrator".

3. Now write your script.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (7)

4. In the guide we will write: "This is a PowerShell script written in Notepad."

5. Go to File in the top menu and click "Save As".

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (8)

6. Choose a name for the PowerShell script and click "Save".

That's it. You have successfully created a PowerShell script using Notepad.

#3.Z PowerShell-ISE

PowerShell on Windows also offers its own integrated scripting environment. You can use it to code your scripts without relying on third partiescode or text editors.

To use it successfully, you need to follow the steps below:

1. Start opening.

2. Now search for the keyword "Windows PowerShell ISE"

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (9)

3. Select the first result, right-click and then left-click "Run as administrator". The PowerShell ISE window opens.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (10)

4. Now click on File and then select "New". A new file window will open as below.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (11)

7. Enter the required script. In the tutorial we will write: "This is a PowerShell script written using PowerShell ISE."

8. When you're done, go to File and Save As.

9. Here, select a file name and click Save.

You can also run the script directly from PowerShell ISE. To do this, click the RUN button at the top of the environment. If it reports a security error, learn how to fix it below.

How to fix and run a PowerShell script security error

As mentioned earlier, Windows has a strict security policy when running scripts. To resolve this, you need to enable scripting on your Windows computer.

To do this, follow the steps below:

  1. Open the Windows Start menu.
  2. Search in PowerShell.
  3. Now right-click on the PowerShell application and right-click on it.
  4. Select the "Run as administrator" option.
  5. Once PowerShell opens, type "Set-ExecutionPolicy RemoteSigned"
  6. Press Enter and then you will be prompted for confirmation. Press A to turn it on.

That's it; now you can finally run all your scripts!

For PowerShell ISE it looks like this.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (12)

For Visual Studio Code it looks like this.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (13)

And for Notepad, you have to run it directly from the PowerShell command line.

How to Create and Run PowerShell Scripts: A Step-by-Step Guide (14)

Best practices for writing and running PowerShell scripts

Follow best practices when working with PowerShell scripts. These practices include:

  • Use the full name of the cmdlet instead of the alias. An alias is only useful when interacting with the console. Alias ​​improves faster interactivity, but can break the script.
  • It is best not to useHost recordscmdlet because it provides limited functionality. For example, it only sends output to the screen. However, it can be used to display text in a specific color. You can use insteadRecording outputcmdleti.
  • Always use the singular form of the cmdlet instead of the plural.
  • Use an approved cmdlet word.
  • If possible, please provide the application extension. Eliminates confusion and improves readability.
  • Use a dedicated PowerShell code editor such as VS Code or PowerShell ISE. They provide excellent features for writing and running PowerShell scripts. You can use Notepad or Notepad++, but then you are limiting yourself.
  • Try to comment, but don't overdo it because it can damage the readability and maintainability of the code.
  • Make sure you set the correct security parameter for running PowerShell scripts.


That's it! You can now create and run PowerShell scripts using Notepad, VS Code, and PowerShell ISE. Additionally, you can use any of the tools listed to write and run PowerShell scripts.

However, as mentioned in the best practices, it is best to avoid writing PowerShell scripts using Notepad or Notepad++, as you lose many of the features offered by the dedicated code editors VS Code and PowerShell ISE.

Then you can also read how to do itcheck the version of PowerShelland update it on the Windows server.


How do I run a PowerShell script step by step? ›

How can I easily execute a PowerShell script?
  1. Browse to the location you stored the ps1-file in File Explorer and choose; File-> Open Windows PowerShell.
  2. Type (part of) the name of the script.
  3. Press TAB to autocomplete then name. Note: Do this even when you typed the name in full. ...
  4. Press ENTER to execute the script.
Mar 3, 2023

How do I write and run a PowerShell script? ›

To create a PowerShell script with Notepad, use these steps: Open Start.
Type a name for the script — for example, first_script. ps1.
  1. Select the folder to save the script file.
  2. Click the Save button.
  3. (Optional) Click the Run button on the top-right side (or press the F5 key) to run the script.
May 9, 2023

What is the easiest way to run a PowerShell script? ›

In File Explorer (or Windows Explorer), right-click the script file name and then select "Run with PowerShell". The "Run with PowerShell" feature starts a PowerShell session that has an execution policy of Bypass, runs the script, and closes the session.

How do I create an automated PowerShell script? ›

How to Automate PowerShell Scripts with Task Scheduler
  1. Step 1: Open Task Scheduler from the Run window.
  2. Step 2: Right-click on the Task Scheduler (Local) in the left panel and click Create Task. ...
  3. Step 3: Add parameters for the task in the Create Task window and click OK to save changes.
  4. Step 4: Switch to the Trigger tab.
Dec 23, 2022

How do I write a PowerShell script from Command Prompt? ›

To run scripts via the command prompt, you must first start up the PowerShell executable (powershell.exe), with the PowerShell location of C:\Program Files\WindowsPowerShell\powershell.exe and then pass the script path as a parameter to it.

How to run a PowerShell script from command line with arguments? ›

Passing arguments in PowerShell is the same as in any other shell: you just type the command name, and then each argument, separated by spaces. If you need to specify the parameter name, you prefix it with a dash like -Name and then after a space (or a colon), the value.

How to run PowerShell script as user in cmd? ›

The trick to running PowerShell on the local machine as a different user is to use Enter-PSSession (see the docs) or Invoke-Command (see the docs) and specify localhost for the -ComputerName . Use Enter-PSSession when running commands interactively, and use Invoke-Command to run code in a script.

What is the simplest PowerShell command? ›

Out-File. Out-File is the simplest PowerShell command to just save the output of your PowerShell cmdlet(s) to a raw text file somewhere on your computer.

What is the best method to learn PowerShell? ›

My #1 pick for the best PowerShell course in 2022 is Microsoft's Automate administrative tasks by using PowerShell learning path. By the end of this free course, you'll be able to create and run script files to automate tedious tasks locally, remotely, and on the Azure cloud.

What tool to create PowerShell script? ›

Visual Studio (VS Code) is the most famous tool among PowerShell scripters. It is the product of Microsoft and available freeware. This tool is available in different OS flavors like Windows, Linux, and MAC.

How do you write a simple automation script? ›

Step 1: Get All the Components Ready
  1. Step 1: Get All the Components Ready. ...
  2. Once you are done arranging these components, you need to configure the Selenium WebDriver along with the Eclipse. ...
  3. Step 2: Sign Up For the BrowserStack Account. ...
  4. Step 3: Choose Automate Tab from the Grid Menu. ...
  5. Step 4: Add the Necessary Codes.

How do I create an automate script? ›

Procedure. In the Automation Scripts application, select the type of launch point that you want to create. Select whether to add a script or specify an existing script. If you add a script, you can code it in a source code field or you can import a script.

How do I run a script from Command Prompt? ›

To run a script with or without a logo
  1. Start the MS-DOS command prompt.
  2. Enter the following commands at the command prompt (modify accordingly if your sample scripts are located in a different folder): Copy Code. cscript //logo c:\"sample scripts"\chart.vbs cscript //nologo c:\"sample scripts"\chart.VBScript.
Mar 23, 2023

How do I run a PowerShell script from a path? ›

To run a script, type the full name and the full path to the script file. To run a script in the current directory, type the path to the current directory, or use a dot to represent the current directory, followed by a path backslash ( . \ ).

How do I run a script from a command? ›

Steps to write and execute a script
  1. Open the terminal. Go to the directory where you want to create your script.
  2. Create a file with . sh extension.
  3. Write the script in the file using an editor.
  4. Make the script executable with command chmod +x <fileName>.
  5. Run the script using ./<fileName>.

What is the difference between PowerShell and command prompt? ›

CMD is the command line for Microsoft Windows operating system, with command-based features. Powershell is a task-based command-line interface, specifically designed for system admins and is based on the . Net Framework.

Where do I write PowerShell commands? ›

New PowerShell cmdlet functions can be written in any text editor or word processing tool. However, the latest versions of the Windows operating system include a tool called the PowerShell ISE (Integrated Scripting Environment) to make scripting even easier and more robust.

How do I start Windows PowerShell from command line? ›

At the Command Prompt

In Windows Command shell, Windows PowerShell, or Windows PowerShell ISE, to start Windows PowerShell, type: PowerShell_ISE . In Windows PowerShell, you can use the alias ise .

How do I create a line in PowerShell? ›

Usage. `n is used in Powershell to append a new line to the standard output. The use of `n in Powershell is similar to \n in C++ or Java. The backtick character (`) represents an escape character in Powershell.

How do I run a PowerShell script as administrator remotely? ›

Click on the Start menu (or the Windows key). Type powershell ise in the search box, and select Windows PowerShell ISE. Press CTRL+SHIFT+ENTER to start the ISE (enter the administrator credentials if prompted).

Is it easy to learn PowerShell scripting? ›

PowerShell is one of the easiest languages to get started with and learn for multiple reasons. As mentioned before, PowerShell follows a "verb-noun" convention, which makes even more complex scripts easier to use (and read) than a more abstracted language like .

Which tool makes it easier to run scripts in PowerShell? ›

Ironman PowerShell Universal

PowerShell Universal is an incredibly useful administration and development tool that helps you build web-based PowerShell scripts and run them all from one spot.

What commands does PowerShell use? ›

Here are 12 of the more widely used PowerShell cmdlets.
  • Get-Command. The Get-Command cmdlet retrieves a list of all the PowerShell commands whose modules have been loaded. ...
  • Get-Help. ...
  • Get-Process. ...
  • Get-Service. ...
  • ConvertTo-Html. ...
  • Get-ChildItem. ...
  • Copy-Item. ...
  • Get-History.
Mar 8, 2023

What does $_ mean in PowerShell? ›

$_ is a variable created by the system usually inside block expressions that are referenced by cmdlets that are used with pipe such as Where-Object and ForEach-Object . But it can be used also in other types of expressions, for example with Select-Object combined with expression properties.

What are the most basic PowerShell commands called? ›

PowerShell commands are known as cmdlets, and these cmdlets are the driving force behind its functional capabilities.

How do I get a list of all PowerShell commands? ›

The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions, filters, scripts, and applications. Get-Command gets the commands from PowerShell modules and commands that were imported from other sessions.

How long does IT take to learn PowerShell scripting? ›

How Long Does it Take to Learn PowerShell? PowerShell is a powerful command-line interface solution for Windows devices. As such, it usually takes around one to two weeks to get a handle on it.

Where can I learn PowerShell scripting? ›

Learn PowerShell Scripting | Udemy.

How to learn PowerShell for beginners? ›

  1. Basic familiarity with using a command-line shell like Command Prompt or Git Bash.
  2. Visual Studio Code installed.
  3. Ability to install Visual Studio Code extensions.
  4. Ability to install software on your computer, if you're not using a Windows operating system.

What can you automate in PowerShell? ›

PowerShell can be used to automate tasks such as user management, CI/CD, managing cloud resources and much more. You'll learn to run commands, how to learn more about PowerShell and additionally to create and run script files.

What are PowerShell scripts called? ›

Windows PowerShell commands, called cmdlets, let you manage the computers from the command line.

How to create PowerShell template? ›

Here's how you do it.
  1. In PowerShell Studio, create a new PowerShell script file (. ps1). ...
  2. Delete the comment-based-help block, so you have a blank page.
  3. Click File and then click Create Template. ...
  4. Type a name for your template file and then click Save.
Apr 22, 2016

How do you write a manual test script? ›

Creating manual test scripts
  1. Open a test case.
  2. From the Test Scripts section of the test case, click the Create Test Script icon ( ).
  3. In the New Test Script dialog box, in the Name field, type a descriptive name that identifies the purpose of the script.
  4. Optional: Type a description.
  5. The type is Manual by default.

What is the simplest form of automation examples? ›

RPA, or Robotic Process Automation, is one of the simplest forms of automation, allowing you to drive better campaign results, manage unlimited workflows, and save time with just a few clicks.

Which tool runs commands and scripts automatically? ›

Microsoft Windows Task Scheduler can help you automatically launch a program or PowerShell script at a certain time or when certain conditions are met.

What scripting language does power automate use? ›

Power Automate for desktop enables you to automate complex scenarios using scripts in VBScript, JavaScript, PowerShell, and Python.

How do I create an automated script in Windows? ›

Get Started Using Windows Task Scheduler
  1. Create Your First Task. Search for “Task Scheduler”. ...
  2. Create an Action. Go to Actions > New.
  3. Add the Python Executable File to the Program Script. ...
  4. Add the Path to Your Python Script in the Arguments. ...
  5. Trigger Your Script Execution.
Oct 9, 2022

How do I start PowerShell from command line? ›

At the Command Prompt

In Windows Command shell, Windows PowerShell, or Windows PowerShell ISE, to start Windows PowerShell, type: PowerShell_ISE . In Windows PowerShell, you can use the alias ise .

How do I run a PowerShell script as administrator from Command Prompt? ›

To run PowerShell as administrator from cmd, Open the command prompt, type PowerShell, and then enter “Start-Process powershell -Verb RunAs”. This will open a new PowerShell window with administrator privileges.

How do I run a simple script? ›

Click the Start button, and then click Run. In the Open field, type the full path of the script, and then click OK. You can also type WScript followed by the full name and path of the script you want to run.

How do I write a command script in Windows? ›

Using a Script CMD 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. ...
  3. Type the following line and press Enter: start "c:\windows\system32" notepad.exe.
Apr 24, 2019

What is script command? ›

The script command makes a typescript of everything displayed on your terminal. The typescript is written to the file specified by the File parameter. The typescript can later be sent to the line printer. If no file name is given, the typescript is saved in the current directory with the file name typescript.

How do I run PowerShell as administrator without prompt? ›

To run PowerShell as administrator via the Run command window:
  1. Press Win Key + R. A a small window will pop up as shown in the screenshot below.
  2. Type in powershell and press Ctrl+Shift+Enter or press and hold Ctrl+Shift.
  3. Click OK to make PowerShell run as administrator.
Nov 11, 2020

How can I run PowerShell as administrator? ›

11 Answers
  1. Create a shortcut to your Powershell script on your desktop.
  2. Right-click the shortcut and click Properties.
  3. Click the Shortcut tab.
  4. Click Advanced.
  5. Select Run as Administrator.

How do I run PowerShell as administrator without admin rights? ›

Running PowerShell Script as a Non-Admin User
  1. Open PowerShell.
  2. Enter the following command: Set-PSSessionConfiguration -Name Microsoft.Powershell -ShowSecurityDescriptorUI. ...
  3. Click Yes to confirm.


Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated: 09/01/2024

Views: 5271

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.