Updated March 4, 2023
Introduction of Start PowerShell from cmd
Starting Powershell or Powershell scripts through command prompt is one of the essential activities of an administrator. The command prompt is a type of interpreter application used to execute commands. It is popularly refered as cmd. It is opened by pressing winkey plus R. There are different ways in which it can be used to start powershell and PowerShell scripts including running the powershell as administrator. Since the command prompt works with commands, it is easy to use and execute various commands. This article will cover in detail on different ways of using cmd to execute Powershell scripts.
Syntax of Start PowerShell from cmd
Below is the syntax to start the Powershell from the command prompt in the following os. This is applicable to Windows® 7, Windows Server® 2008 R2, and Windows Server® 2008.
- Open the command prompt by pressing winkey + R. Type Cmd
- Once the command prompt is open, type PowerShell
To start the PowerShell ISE in the following os Windows® 7, Windows Server® 2008 R2, and Windows Server® 2008.
Open the command prompt by pressing winkey + R. Type Cmd
Once the command prompt is open, type PowerShell_ISE
Instead of PowerShell_ISE, ISE alone can be used.
To open 32-bit version of PowerShell the following can be run from the command prompt
To open the PowerShell 32-bit in windows server 2012
Open the command prompt
To open the PowerShell 32-bit in windows 8.1 and windows 8
Open the command prompt
To start the PowerShell with run as administrator mode
Open the command prompt
Start-Process PowerShell -Verb RunAs
To run a PowerShell file from command prompt type the following
powershell.exe “& ‘c:\Data\test.ps1′”
In some cases, it may throw an error, in that case, the execution policy should be set to remote signed.
If the script is in the current directory that is there in the command prompt, then ./test.ps1 will run the script. If there are spaces involved in the path of the file, then entire path must be enclosed within double quotes and an ampersand symbol preceding it.
Examples of Start PowerShell from cmd
Following are the examples are given below:
Executing a PowerShell Script from cmd in windows 10: Before executing a script, the script needs to be developed first. Let’s consider a script that will show the following output, “File is successfully run from the command line”. Open notepad and type the following line.
Write-Host “File is successfully run from command line”.
Save this file with .ps1 extension.
There is also another way of creating a script file. Open Windows PowerShell ISE. Click on the file option and select a new file. Type, Write-Host “File is successfully run from the command line”. Save the file.
The above is the input file. To execute the above file from the command prompt, follow the below steps.
- Press winkey+ R
- Type cmd
- The command prompt will be opened.
- Type PowerShell and press enter.
Then type & “C:\Vignesh\Script\test.ps1” and then press enter. The script will be executed, and the output will be shown as below.
Executing a script after receiving input from the user
Write-Host "File is successfully run from command line"
$uname=Read-Host "Enter ur name"
$age=Read-Host "Enter ur age"
$city=Read-Host "Enter ur city"
Write-Host "the entered name is" $uname
Write-Host "the entered age is" $age
Write-Host "the entered city is" $city
To run the above script, type PowerShell in command prompt. Then & followed by the path of the script in double quotes and press enter. It will ask for a name, followed by age and Chennai. Once the parameters are passed, the given input will be printed as shown above. In case if an error is thrown because of the execution policy, then the execution policy needs to be modified using a set-execution policy cmdlet.
Executing scripts with input parameter: In many cases, the script may require inputs from the user to perform some calculation or execution. At that time, the input needed from the users is configured as parameters in the script. This is achieved with the help of args keyword. While declaring parameter, it is possible to define even their data type as string, int, etc. In case if the data type is not known, we can ignore this part. This means that the argument can accept both letters and numbers. If a script has a parameter and is executed without parameters, it will either not run or may not produce the desired results. The below example will show how to pass parameters from the command line to a script.
Write-Host "The user name is" $username
Write-Host "Age of the user is" $age
Write-Host "city of the user is" $city
Write-Host "The user's wife name is" $wname
Write-Host "Age of the user's wife is" $wage
Write-Host "city of the user's wife is" $wcity
$toal= $age + $wage
Write-Host "total age is 58"
The above script has six parameters. If you can see from the command prompt, the six parameters are passed. Since the parameters are positioned, it is must to know that the parameters must be passed in the same order else the output will be screwed. In case if the parameters are specified with a data type, that must be also kept in mind while supplying them. In the above case, the parameters are of string type. If the parameters are data typed, if there is a mismatch while passing the parameters, then an error will be thrown. Even if the order of the parameters is changed, then the script won’t execute as expected. The biggest disadvantage of running a script from cmdlet is that it is not possible to debug a script when developing and executing a script as keeping break points is not allowed and possible from the command prompt. It is advisable to use command prompt to run scripts only after thorough testing of the credibility of the script.
Thus, the article covered in detail about running PowerShell scripts from the command prompt. It covered with examples on various scenarios on running a script such as, without parameters, with the script prompting the user for input, running a script with parameters. To learn more in detail it is advisable to write sample scripts and practice them.
This is a guide to Start PowerShell from cmd. Here we also discuss the introduction and syntax of start powershell from cmd along with different examples and its code implementation. You may also have a look at the following articles to learn more –