A PS1 compiled   to EXE does not recognize the passed parameters.

Posted on 2014-04-01
Last Modified: 2014-04-09
Powershell Newbie here.

Using PowerGUi Script Editor, I compile a PS1 to EXE.   When I run the EXE it does not see the passed parameters.  I am running them from the PowerShell Console in the PowerGUI Script Editor.

Here is the program code.

param([string]$foo , [string]$bar )
Write-Host "Arg0: $foo"
Write-Host "Arg1: $bar"
Write-Host "Arg2: $who"

Here are the test results
--Calling EXE--
PS C:\test> .\blat2.exe it bugs me

--Calling PS1--
PS C:\test> .\blat2.ps1 it bugs me
Arg0: it
Arg1: bugs
Arg2: me

Being a newbie, I'm pretty sure I am making a rookie mistake.  
How do I get the exe to recognize the passed parameters?
Question by:HCSHAW
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39971584
In your program code, did you forget to type the last parameter in the param list?  $who
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39971819
Try this :
 .\blat2.exe -arguments -foo "it" -bar "bugs"  -who "me"

Author Comment

ID: 39973117
Yes I did forget to show the Parameter $Who.  It was a typo.

Corrected to read param([string]$foo , [string]$bar ,  [string]$who )

The program  is being called by a software that I have no control over, so I cannot change the way it calls blat2.exe.

A typical command line would be  

blat2.exe -t something  anotherthing  

So the expected parameters would be  


So blat2 has to accept the command line as it is sent, I cannot change the program that calls  blat2.exe

My issue is that I can get the $args to show up if it is is PS1 run, but after compiling they quit showing up.  It's like the compile is preventing the program from accepting the parameters.

Why does the EXE version not  recognize the incoming parameters?
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.


Author Comment

ID: 39983276
I have tried this with the -arguments option and it works.   However, this does not help me solve the problem I have.

.\blat2.exe -arguments -t somthing something else

The software that is calling the blat2.exe cannot be modified to add the -arguments into the call.

Is there any other way to get the exe to accept the args passed without having to add the in the -arguments to the call?
LVL 70

Accepted Solution

Qlemo earned 500 total points
ID: 39983644
Bad news. I've performed some tests, and it seems you cannot get around that "limitation". Anything provided and not being a subset of the PowerShell.exe parameters (like -NoExit, -NoBanner) will just be ignored. Even $myInvocation does not have anything useful here.

Good news. You can get the commandline, and remove the script/exe name:
$ownarg = [Environment]::GetCommandLineArgs()
write-host $ownarg[1..$ownarg.Count]

Open in new window


Author Comment

ID: 39984199
That seems very promising.  I'll give that a try here shortly.  I have it working in test, but am testing with the real code.

Author Comment

ID: 39985398
I've requested that this question be closed as follows:

Accepted answer: 0 points for HCSHAW's comment #a39984199

for the following reason:

That worked!  Thanks for that.  I have looked everywhere for some way to do this, only to be stumped again and again.

Here is my final code for those that are following up on this later.

SOURCE CODE ----------------------------------------------------
$ownarg = [Environment]::GetCommandLineArgs()
& cmd.exe /C "C:\TEST\Blat2.exe"  $ownarg[1..$ownarg.Count]

I then complied it using POWERGUI to an EXE
LVL 70

Expert Comment

ID: 39985399
Please accept my comment instead of yours.
And, as said, the call to the batch file is unnecessarily complicated.

Author Comment

ID: 39986026
Sorry about that.   Thanks for getting it corrected.
I understand the call to the batch is complicated, but I am kinda stuck right now with what I have.   This works and buys me some time.   Thank you again for you help.
LVL 70

Expert Comment

ID: 39987291
Again, to be clear:
& cmd.exe /C "C:\TEST\Blat2.exe"  $ownarg[1..$ownarg.Count]

Open in new window

can be written as
C:\TEST\Blat2.exe $ownarg[1..$ownarg.Count]

Open in new window


Author Comment

ID: 39988765

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question