Young student studying online through laptop during the new norm
Young student studying online through laptop during the new normal digital remix

Run PowerShell script | a clear tutorial made for beginners

PowerShell is the language, when it comes to automation of tasks and configuration in the Microsoft cosmos. Meanwhile, you can use it cross-platform, e.g., on Linux systems. This article intends to show you in few steps how to run a PowerShell script.

There are many possibilities to run a PowerShell script. I want to show you the ways I know. If I forgot something, write me a mail: Serkar@sposcripts.com.

Scenario

In the first step, I want to describe you, which script I run. For this purpose, I am starting the PowerShell ISE by clicking on the start menu button and typing ISE. PowerShell ISE is the preinstalled tool, where you can script your PowerShell scripts.

Click on windows start button
image 41

After clicking on Windows PowerShell ISE, a window pops up.

The PowerShell ISE is structured in two panes — the script and command pane. In the script pane you can develop your PowerShell script and if you run this developed script, its output will be displayed in the command pane. You can also use the command pane to run cmdlets interactively.

PowerShell ISE structure

For my scenario, I am writing a code, where I prompt the user how the weather is. If you are seeking to learn something, I would recommend you to join me.

PowerShell ISE with PowerShell Script

Source code:

Read-Host "How is the weather?"

Save this script to your desktop, so you can find it later.

Save as in PowerShell ISE

Run PowerShell script from ISE

When you want to run a PowerShell script directly from ISE, you have two possibilities:

You can run a PowerShell script by clicking on the F5 button. As you can see, the prompt came up in the command pane.

Result of the PowerShell script run

I am stopping the script with the stop button

Stop the PowerShell script

You can also run the script, by clicking on the play button.

Run PowerShell script with play button

Run PowerShell script from explorer

If we want to know how to run PowerShell script from explorer, we have to browse to our previously written PowerShell script, right-click the file and click on Run with PowerShell. A PowerShell window will pop up which runs the PowerShell script.

Screenshot of run with PowerShell

Output of the PowerShell script in Windows PowerShell

Run PowerShell Script from PowerShell

You also have the possibility to run PowerShell scripts directly from PowerShell. To do this, open Windows PowerShell.

Again, I am clicking on the Windows start button and type in PowerShell. The search result will show me Windows PowerShell, which I want to start.

image 50

Now I am getting the path of my file by using the keyboard combination of shift and mouse right click to copy the path.

Copy as path option of windows explorer

I am switching back to Windows PowerShell and press right click of the mouse to paste in my clipboard.

pasted path of the powershell script

Run PowerShell script – the standard way

I am removing the quotes and press enter. As you see, the PowerShell script was started:

image 51

Run PowerShell script – the dot sourcing

You can also start your PowerShell script by adding a dot before the quoted path. This is called dot sourcing and it runs the contents of the script in your current context.

image 54

Run PowerShell Script from Command Prompt

You may know Command Prompt — the predecessor of PowerShell. You can also run PowerShell scripts in the Command Prompt. To run PowerShell scripts in Command Prompt, start Command Prompt, by pressing the windows button and typing command prompt. Click on the app and a black box should open.

Screenshot of Windows Menu with Command Prompt in it

Now you can start your script like this:

Start PowerShell.exe “pathtoyourscript”

image 62

You will notice, that PowerShell will open in a separate window and run your specified script:

Screenshot of PowerShell session started from Command Prompt

Run PowerShell script from a folder

Run PowerShell script from a session, opened in script location

You can also run a PowerShell script if you open a folder in Windows Explorer and start a PowerShell session there:

So navigate to your folder and click on file

Windows explorer with a bar towards file

Now click on Open Windows PowerShell

Open Windows PowerShell option from the file context menu

You might notice, that the position is showing now the path, where the session has been started.

Changed Path in Windows PowerShell

If you enter a dot now, you can press the tabulator button and tab through each file in your path until you find your PowerShell script. My desktop is a bit messy, but after a while I could find the demo. By pressing on enter, the PowerShell script runs.

PowerShell script started from localized PowerShell session

Troubleshooting

You might experience, that you cannot start a PowerShell script, because PowerShell states, that your script, which you try to run isn’t digitally signed. In this case, the execution policy of PowerShell blocks you from running scripts. Its purpose is to protect your computer from malicious scripts.

In this case, read the script first and if you can confirm, that it is not harmful, you can unblock the script by following cmdlet:

Unblock-File -Path "C:\Users\Serkar\Desktop\Demo.ps1"

For more, please read the documentation of Microsoft:

Set-ExecutionPolicy (Microsoft.PowerShell.Security) – PowerShell | Microsoft Docs

Conclusio

I hope that you have learned how to run a PowerShell script. Give it a try and try to run PowerShell scripts from by using various methods.

Further Reading

You may also want to get to know in detail what is PowerShell. I can recommend the docs of Microsoft, which give you a good understanding of PowerShell: What is PowerShell? – PowerShell | Microsoft Docs

Picture Reference

Schule Foto erstellt von rawpixel.com – de.freepik.com

Leave a Reply