Powershell

How to run a PowerShell script from command line

PowerShell is a command line application and provides graphical support from a scripting language. PowerShell scripts are created to perform various tasks in bulk. PowerShell scripts can be created by using the PowerShell ISE support or any text editor.

The files that contain “.ps1” refer to the PowerShell scripts. PowerShell script can be executed from the PowerShell itself, or from the GUI of the operating system. This article provides the sequential support to run the PowerShell script from the command line support of windows.

How to run a PowerShell script from command line

PowerShell scripts can be executed from the command prompt and the PowerShell terminal. Both are the command line support provided by the Windows. This section describes both methods to run a PowerShell script.

How to run a PowerShell script from cmd (Command Prompt)

Cmd (Command prompt) is the CLI of windows to interact with your machine through commands. The following syntax should be followed to run the PS script from the command prompt.

powershell -file "path/of/PowerShell/script"

The “powershell” keyword is used with a “-file” option to execute the script placed at the specific location.

Step 1: Open the command prompt from the windows search bar

Graphical user interface, text, application Description automatically generated

Step 2: Once it is opened write the command with the path of the script as we did here.

powershell -file "E:\alias.ps1"

Text Description automatically generated

The script code will be executed as shown in the above image.

How to run a PowerShell script from PowerShell terminal

PowerShell is a more advanced version of cmd. PowerShell has its own set of commands known as cmdlets and it also provides scripting language support.

Step 1: Open the PowerShell from the search bar of windows

Graphical user interface, application Description automatically generated

Step 2: The first and foremost step to execute a PowerShell script is to set the execution policy to RemoteSigned value. The execution policies provide a safe and secure execution of programs, scripts, and any application. The Set-ExecutionPolicy cmdlet is exercised to change the execution policy of PS to RemoteSigned:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Text Description automatically generated

Step 3: You need to either provide the relative path of the script or the absolute path to run the script. The following command executes the “alias.ps1” script by using its absolute path:

Note: It is recommended to use the absolute path of the script to avoid any path related errors.

E:\alias.ps1

Text Description automatically generated

The output shows that the script has been run successfully from the PowerShell.

Conclusion

The command-line interfaces such as PowerShell and command prompt can be used to run the PowerShell script. PowerShell and cmd are the command line interfaces to perform tasks on windows. This article demonstrates the methods to run the PowerShell script from the terminal. PowerShell is the advanced version of cmd as it provides scripting language support and commands.

About the author

Adnan Shabbir