MicrosoftMicrosoft TeamsPowershell

How to run PowerShell script

PowerShell is an interactive shell developed by Microsoft mainly for System Administrators, especially to replace CMD.EXE, a great tool of its time. CMD is very limited for the current time and needs of System administrators, and it was the main reason behind the birth of PowerShell. When you start exploring the PowerShell in deep, it will quickly become evident that it is way more potent than an interactive shell. System administrators can manage almost anything with PowerShell like Server Operating Systems, Exchange Servers, Active Directory, Office365, Azure, and many more.

Windows PowerShell and PowerShell Core:

Soon after the success of Windows PowerShell, Microsoft realized that they need PowerShell to be cross-platform, which can operate on any Operating System like Mac, Linux, Windows, so they stop developing the Windows PowerShell, which runs only on Windows Operating systems, and start creating a cross-platform version of PowerShell called PowerShell Core.

There are two types of PowerShell one called Windows PowerShell’s latest build version 5.1, and the other is PowerShell Core latest build version 7.2.

How to check PowerShell Versions.

There are many ways to check the PowerShell version installed on your Operating System, but I will show you the easiest way which will work on both versions of PowerShell “Core and Windows Powershell..

Run the \$PSVersionTable command “cmdlet” in any version/type of PowerShell to know which version/type of PowerShell is running on your computer.

Recommended For You:  How to Convert PNG to JPG in Bulk Using PowerShell
$PSVersionTable
$PSVersionTable

How to Open PowerShell:

Getting started with PowerShell is a straightforward matter of running PowerShell.EXE instead of CMD.EXE. To launch the PowerShell interactive shell, click on the search bar, and type PowerShell in it, and click on the PowerShell app as shown in the image below.

You can also launch the PowerShell from the Command Prompt by typing PowerShell in the command prompt and hitting the Enter button on your keyboard.

How to create a PowerShell script:

Before I go ahead and show you how to run the PowerShell script, we need to create a PowerShell script first, and for creating a PowerShell script, you should have little knowledge about basic PowerShell commands (cmdlets). To create a PowerShell Script, you can use any text editor like Notepad or an IDE “Integrated development Environment,” for example, PowerShell ISE or Visual Studio code.

In my case, I will use Notepad to create a PowerShell script. If you want to follow along, open up your text editor or any IDE and type Get-Service, a basic PowerShell command that shows all the running services on any computer.

PowershSll script example code:

Get-Service

After that, save the file with the .ps1 extension to create the PowerShell script.

Save Powershell Script

How to Run PowerShell Script from PowerShell console:

Running the PowerShell script from the PowerShell console is very easy. Launch your PowerShell console and navigate to the location where you saved your PowerShell script with .ps1 extension, add “.\” before the file name, and hit enter to execute the PowerShell script, as shown in the image below.

Run Powershell Script From Powershell Console
running scripts is disabled on this system

Running scripts is disabled on this system:

PowerShell is a very secure scripting language, and it was developed by keeping in mind all different kinds of security threats. That’s the reason you cannot run any PowerShell script until you understand PowerShell script execution policies.

PowerShell Script Execution Policies:

Restricted: Scripts will not run. (Default Policy)

RemoteSigned: You will be able to run Locally created scripts, but scripts created on different machines/computers will not run unless a trusted publisher signs them.

AllSigned: Scripts will only run if signed by a trusted author (including locally created scripts).

Unrestricted: All scripts will get execute despite who developed them and whether they are signed or not.

In our case, the RemoteSigned PowerShell script policy will work well, so let’s set our PowerShell script policy to RemoteSigned by enter the following command in your PowerShell console.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
set-execution policy

After changing the PowerShell script execution policy, the script will run without an issue, and you will not get the error “running scripts is disabled on this system,” as shown in the screenshot below.

running get-service script

To learn more about PowerShell script execution policies, execute the following command in your PowerShell console “Get-help about_Execution_Policies.”. About_Execution_Policies cmdlet will provide you complete in-depth detail about PowerShell script execution policies.

about execution policies

Run PowerShell script from command line:

To run the PowerShell script from the command line, you need to call PowerShell.exe with the script Execution Policy parameters and provide the script file path as shown in the image below.

run powershell script from command prompt

Open PowerShell from cmd (Command Prompt):

You can also open PowerShell from cmd (Command Prompt) by typing PowerShell.exe in the command prompt, as shown in the image below.

open powershell from cmd

Run PowerShell script as administrator:

If you want to run the PowerShell script as an administrator, execute the PowerShell console as administrator before running the script, and the script will run with administrative privileges. You can check the below image for reference.

If you want to learn more don’t forgot to bookmark and share MCSAGURU.

Muhammad Faizan

Hi, My name is Muhammad Faizan and i have spent last 15 years working as System Administrator mainly with Microsoft Technologies. I am MCSE, MCTP, MCITP, certified professional. I love scripting and Powershell is the scripting language i am in love with.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button