How to execute powershell.exe with script and parameters

posted in: Powershell | 0
Background

I had some problems today powershell.exe and the syntax.

My goal was to use an application that executes a script together with parameter to start an installation.

This is how to execute powershell.exe properly using command prompt.

What I tried to do

I wanted to run Powershell.exe from the command line and supply it with arguments, a file and parameters.

First I was trying to use something like this

powershell.exe -ExecutionPolicy Bypass -File “Install-Application.ps1 -Mode Install” -WindowStyle Hidden -NoProfile

I couldn’t get this to work and the CM logs didn’t tell me what the issue was.

What the issue actually was

I started a command prompt and tried to use the string above and got this result

Processing -File ‘Install-Application.ps1 -Mode Install’ failed because the file does not have a ‘.ps1’ extension.
Specify a valid Windows PowerShell script file name, and then try again.

I moved the quotes to only include the file name and got this error instead

C:\Temp\Visual Studio Professional 2015 with Update 3\Install-VSPro2015U3.ps1 :

A parameter cannot be found that matches parameter name ‘NoProfile’.

It turns out that the -NoProfile argument has to come before the file and parameters.

This is the string that finally worked.

powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -File “Install-VSPro2015U3.ps1” -Mode Install

Takeaways

Always supply the arguments to PowerShell.exe before the script and any parameters, otherwise powershell will consider them to be part of the filename and fail.

In my case I had to move -NoProfile and -WindowStyle Hidden to before the -File argument.

This is especially important when using SCCM and Powershell to deploy applications to clients.

The only clue I had to troubleshoot this was the failure message after starting the install from Software Center.

It show gave me the exit code 0xfffd0000 which didn’t really say much but since the download managed to finish and the first error in AppEnforce.log was this I managed to figure it out.

I hope this post saves you some headache!

Leave a Reply

Your email address will not be published. Required fields are marked *