0

Hi all,

I am a beginner with powershell. I created a MyPoweshell.ps1 file as follows:

MyFunction1
MyFunction2

Function MyFunction1 {

}

Function MyFunction2{

}

1) When creating a .cmd file to run the script nothing seems to work except if I typed in the cmd console: Import-Module .\MyPoweshell.ps1. But if I save that code in .cmd and double click the cmd it immediately close. How to run powershell from cmd by double click on it without closing the screen ?

2) Trying to make a clickable shortcut that shows in .cmd but nothing works. Example : C:...\Program Files\MyFolder. How to create a clickable shortcut ?

I appreciate any help. Thanks.

1 Answer 1

2

You are trying to call a powershell function from CMD? See whats wrong here?

To call a powershell script from CMD enter the following:

powershell -file C:\yourscript.ps1

Also you need to define the functions before you call them so in yourscript.ps1 you would need to have this

Function MyFunction1 {
   # Do Stuff
}

Function MyFunction2{
   # Do Stuff
}

MyFunction1
MyFunction2
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Paul, your answer didnt seems to work, I am getting errors "the term 'MyFunction1' is not recognized as cmdlet ..etc.
@loli you call the functions before defining them, put the function calls at the end of your script
At some point, when you will completely let go CMD, and use Powershell fully, from a Powershell console, you will see how powerful it is. Paul is trying to show you how to call it from CMD.
Thanks Paul that solve it. Just waiting for the shortcut answer (2).
@loli SO is for help with code and not a replacement for doing basic research but here you go support.microsoft.com/kb/140443

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.