Passing variables as arguments in powershell script

Have problems passing variables as arguments when running start-job in powershell script.
This is an example script to shed light on the nature of the problem.

A script like this:

param($directory)
start-job -Name $directory -scriptblock {dir $directory > C:$directory.txt}

When run by its name “ShowDir.ps1 MyDir”

Results in:

Id Name PSJobTypeName State HasMoreData Location Command


79 MyDir BackgroundJob Running True localhost dir $directory > O:\PS…

I can see that the variable $directory works fine in the -Name parameter but not in the -scriptblock. So the actual command line to the job becomes “dir $directory…”

What can I do about this?

Any hints appreciated.

In your Start-Job you need to specify your arguments:

param($directory)
start-job -Name $directory -scriptblock {dir $directory > C:$directory.txt} -argumentlist $directory

Take a look at: Passing variables as arguments in powershell script

Also when you post code could you format it as code: Posting Guide

Thanks for your answer, it did not work out of the box for me. But it set me in the right direction, gave me some new energy to search for the solution, and made me realize a few things.

The first thing I got to work as expected was:


param($directory)

start-job -Name $directory -scriptblock {dir $args > C:\$args.txt} -ArgumentList $directory

Then I realized that the script I really want to do needs more than one parameter put into the script block. Trying :


param($directory, $outfile)

start-job -Name $directory -scriptblock {dir $args > C:\$args.txt} -ArgumentList $directory,$outfile

That did not work as expected. The resulting $args was concatenations of $directory and $outfile Also tried args[0] and args[1], but no.

Then I found: $using:variable that I used like this:


param($directory, $outfile)

start-job -Name $directory -scriptblock {dir $using:directory > c:\$using:outfile.txt}

That gave me the result I needed.

So far so good. But it didn’t end here. The script that I want to do shall run a node.js script requiring parameters (very much simplified) like this:

( $topic is not a variable btw :slight_smile: )


node script -p '$topic/sometopic/+/+/name' -p '$topic/someothertopic/resource/#'

And I want a Powershell script that I can run like this:


./MyPSScript.ps1 sometopic someothertopic

I Tried:


param($sometopic, $someothertopic)

start-job -Name $sometopic -ScriptBlock{node script.js -p '$topic/$using:sometopic/+/+/name' -p '$topic/$using:someothertopic/recources/#'}

Did not work.

Finally, the solution was two Powershell scripts.

First script1.ps1:


./script1.ps1 sometopic someothertopic

Like this:


param($sometopic, $someothertopic)

$tmp="!topic/" + $sometopic + "/+/+/name"
$parm1=$tmp.replace('!', '$')
$tmp="!topic/" + $someothertopic + "/recources/#"
$parm2=$tmp.replace('!', '$')

# Calling a second script.

./script2.ps1 $parm1 $parm2

And script2.ps1 doing:


param($param1, $param2)

start-job -Name $param1 -ScriptBlock{node script -p $using:param1 -p $using:param2}

And :boom: this (and a lot more in the actual real script) work as expected :grinning:

Is this the right way to do it or is there a better way?

If your answer gets flagged as SPAM please do not re-post a second one. Have a little patience for the admins or moderators to release your incorrectly blocked post.

Thanks in advance

Yes, there was an error in my first reply post. And I was not really familiar with the edit/delete/undelete features. So it became a bit messy - sorry. But now it should be right.

Really? There are both posts now. You may delete the one you one you think became messy.

When I am looking one is marked as deleted.

Ah … I see … :+1:t4: