EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial Start PowerShell from cmd
Secondary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Start PowerShell from cmd

By Priya PedamkarPriya Pedamkar

Start PowerShell from cmd

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • 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

Type,%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

To open the PowerShell 32-bit in windows 8.1 and windows 8

Open the command prompt

Type,%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

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.

Set-ExecutionPolicy RemoteSigned

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:

Example #1

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.

Input:

Start PowerShell from cmd-1.1

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.

Start PowerShell from cmd-1.2

Example #2

Executing a script after receiving input from the user

Input:

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

Output:

Start PowerShell from cmd-1.3

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.

Example #3

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.

Input:

$username=$args[0] $age=$args[1] $city=$args[2] $wname=$args[3] $wage=$args[4] $wcity=$args[5] 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"

Output:

Start PowerShell from cmd-1.4

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.

Conclusion

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.

Recommended Articles

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 –

  1. PowerShell Sleep
  2. WinRM PowerShell
  3. PowerShell Get-Service
  4. PowerShell Modules
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more