0

I think that my question is something too easy that you guys will solve in 1 minute.

I'm trying to run a script that have multiple lines of code. But, when I write the first line and hits SHIFT+ENTER it runs the code. I need to write a new line, instead of running what I've wrote.

Anybody knows what should I do (instead killing myself because I'm too dumb) ?

5
  • 3
    Use PowerShell ISE instead of the console! Commented Oct 3, 2018 at 17:12
  • Or use VSCode. Or if you really, really want to enter multiple lines in the console, use the backtick character at the end of the line. Commented Oct 3, 2018 at 17:19
  • What version of PowerShell are you running? Commented Oct 3, 2018 at 17:20
  • What command are you using that takes more then one line? Commented Oct 3, 2018 at 17:28
  • @VivekKumarSingh Thank you so much!! Solved my problem Commented Oct 3, 2018 at 18:28

2 Answers 2

1

In powershell console there are a few ways to make a new line

A. Shift + Enter : Use this at any point to make a new line

B. The opening of a string " or ' until the closing of the string " or ' : use this when you have a string that you wish to span many lines

C. A pipe | : Use this if you have output that you would like to pass to another command

D. The Back tick (escape char) ` : use this to separate lines for a new command or splitting a command into other lines

If you are new to powershell, I would suggest using Powershell ISE. If its installed you can go to the powershell console and type ISE or go to start and type Powershell ISE. This will be a good place to run scripts and debug as you can add breakpoints to your scripts.

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

Comments

0

The easiest and best way to do this would be to create the script inside of the PowerSheell ISE program. You can then reference this script and run it in the console by preceding it with a .\script.ps1.

If needed you can create script on the command line by creating and writing to the file from the console.

  1. Open the PowerShell console
  2. Run the following command to create a file New-Item script.ps1
  3. Run the next command as many times as it takes to populate the file Add-Content script.ps1 "My code line here"
  4. Run the code using the script run command .\script.ps1

Now let it be known that the ISE is a much better tool because it allows for debugging of files and testing them on demand. The only downside is it will cache whatever it uses or creates (such as variables or references). If you aren't getting the expected result trying closing and reopening to clear the cache run it from the console in tandem. One last thing to note is that if you use the ISE and it successfully runs there that doesn't mean it will run in the console. Be sure to test thoroughly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.