We help IT Professionals succeed at work.

Powershell script to call and pass arguments to another Powershell script

DarthElmo asked
Last Modified: 2012-06-01

With the help of Experts here, I've written a Powershell script that remotely executes another Powershell script. The only problem is that the called Powershell script is executing without successfully accepting the argument I'm passing (which is a partial directory path).

Here's the command from the calling PS script:
Invoke-Command { powershell.exe <full path of PS script on remote server> } -ComputerName <server name> -Credential <authentication string> -ArgumentList <directory listing>

Here's the first line in the called PS script:
$<variable name> = Write-Host $args[0]

I'm writing the value of the variable to a log file and the value is empty. Any pointers for me?

Watch Question

You'll have to create a proxy cmdlet: see here.

What about this?

Invoke-Command {param($ScriptPath,$ArgPath) powershell.exe $ScriptPath $ArgPath} -ComputerName <server name> -Credential <authentication string> -ArgumentList <script path>,<directory listing>


Jornak, I tried reading the article you linked but am pretty overwhelmed by it. This is my first Powershell script. I'd really appreciate your help in applying the article to my specific problem if you have the time.

Thank you!


Vaulden, I tried your suggestion but am receiving the error that $ScriptPath and $ArgPath are "not recognized as the name of a cmdlet, function, script file, or operable program." The $ScriptPath should be the full path to the Powershell script on the remote server, yes? And the $ArgPath should be the argument I'm passing (which in my case is the partial path), yes?

Thank you!
This one is on us!
(Get your first solution completely free - no credit card required)


It seems like we're really close, Vaulden. When executing the calling PS script, the PS Console now displays the passed argument - the partial directory - where it was not before. The very first line of the called PS script is:
$Dir = Write-Host $args[0]

Since the calling PS script does not echo that directory, it must be coming from the Write-Host command in the called PS script. However, as the called PS script continues, it behaves as if $Dir is an empty string and therefore ultimately fails. Is "Write-Host $args[0]" an incorrect way to assign a passed argument to a variable?


Well, I decided to simply try $Dir = $args[0] and it looks like the value was successfully assigned to $Dir. I've got a few other errors to follow up on but they do not appear to be related.

Write-Host sends it to the screen, I had meant to ask about that earlier. You are correct in that $Dir  = $args[0] is the correct way to assign the variable.


Well, the argument is definitely being passed successfully and as a result, the variables in the called PS script are correctly assigned. I would have thought I was home free. However, everything fails after that. It's got to be a permissions issue.

The script says it can't find files when in fact the directory is valid and the file exists, for example. I thought the called PS script would be executed by the same ID that is remotely connecting to the server but the called PS script is behaving as if the ID executing it has no permissions to do anything.

Now if I execute the same script from the actual server where the PS script resides and hard code the one parameter it would need if it were being called, then everything executes exactly as I would expect.

Should I go ahead and accept the above solution and create a new question for this latest development?

It's up to you, I'll be willing to help either way. However, if we continue on this ticket someone searching for an answer to the new issue may not find this ticket (since the original question does not pertain).


Thanks so much for your help, Vaulden! I'm opening a new question for this last development.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.