Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to condense PowerShell script to fit on a single line

Quick question. I am trying to write the following PowerShell script, but I would like it to fit on a single line:

$app = New-Object -comobject Excel.Application
$wb1 = $app.Workbooks.Open("C:\xampp\upload_files\Launchpad.xlsm")
$app.Run("Refresh")
$wb1.Close($false)
$app.Quit()

The pseudo-code would look something like this:

$app = New-Object -comobject Excel.Application AND $wb1 = $app.Workbooks.Open AND "C:\xampp\upload_files\Launchpad.xlsm") AND $app.Run("Refresh") AND $wb1.Close($false) AND $app.Quit()

The reason I want to fit on a line is because I would like to insert the arguments directly in the 'arguments' box of Windows Task Scheduler. The reason for this is that for some reason scripts have been disabled (e.g. I cannot call a .ps1 file...)

I know this will still work, as I already have a "one liner" PS script running. What would the syntax look like??

Kind regards, G.

like image 372
Noobster Avatar asked Mar 07 '14 19:03

Noobster


People also ask

How do you wrap a line in PowerShell?

The correct answer is using backtick ` followed by newline.

How do I run multiple PowerShell commands in one line?

Chaining multiple PowerShell commands in one line However, if you want to do run multiple unrelated commands on the same line, you can use the firstcommand; secondcommand method. In this case, the second command gets executed even if the first command fails.

How do I align codes in PowerShell?

Put the caret where you want to indent Press Shift + Alt + Arrow Up or Down and it will create a line vertically across your rows of text. Press Tab and it will indent all those lines. This lets you indent whole code blocks and functions.

How do you make a straight line in PowerShell?

The pipeline character in Windows PowerShell is the vertical bar (also called the pipe: | ). On most U.S. keyboards, it is found on the key with the backslash. You can press Shift + Backslash to get the pipe character.


1 Answers

Powershell statements can be separated with semicolons:

$app = New-Object -COM 'Excel.Application'; $wb1 = $app.Workbooks.Open("..."); ...

The PowerShell executable takes a -Command parameter that allows you to specify a command string for execution in PowerShell:

powershell.exe -Command "stmnt1; stmnt2; ..."

To run this via Task Scheduler you'd put powershell.exe into the "program" field and -Command "stmnt1; stmnt2; ..." into the "arguments" field of the task.

However, as @alroc said: you should verify why script execution has been restricted. If it's just the default setting you can simply change it by running Set-ExecutionPolicy RemoteSigned or override it by adding -ExecutionPolicy ByPass to a PowerShell command line. However, if the setting is enforced by policy changing/bypassing the setting will fail, and you could get into quite some trouble for violating company policies.

like image 95
Ansgar Wiechers Avatar answered Sep 23 '22 08:09

Ansgar Wiechers