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: [email protected].
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.
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.
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.
Read-Host "How is the weather?"
Save this script to your desktop, so you can find it later.
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.
I am stopping the script with the stop button
You can also run the script, by clicking on the 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.
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.
Now I am getting the path of my file by using the keyboard combination of shift and mouse right click to copy the path.
I am switching back to Windows PowerShell and press right click of the mouse to paste in my clipboard.
Run PowerShell script – the standard way
I am removing the quotes and press enter. As you see, the PowerShell script was started:
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.
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.
Now you can start your script like this:
Start PowerShell.exe “pathtoyourscript”
You will notice, that PowerShell will open in a separate window and run your specified script:
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
Now click on Open Windows PowerShell
You might notice, that the position is showing now the path, where the session has been started.
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.
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
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.
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