user2909630
user2909630

Reputation: 13

powershell start-job doesn't do anything with scriptblocks

I'm trying to start a job that creates a file. But it doesn't.

here's the very simple code:

start-job -ScriptBlock { "hi" | set-content "hi.txt" }

The file "hi.txt" never gets created.

Why not?

Upvotes: 1

Views: 2782

Answers (1)

Keith Hill
Keith Hill

Reputation: 202092

It is most likely creating the file, just not where you expect because you used a relative path. Try executing this to see where the file is going:

Start-Job {$pwd} | Receive-Job -Wait

Better yet, use an absolute path:

Start-Job { 'hi' > c:\hi.txt }

Or pass in the desired path:

Start-Job {param($path) 'hi' > "$path\hi.txt"} -arg $pwd

Upvotes: 4

Related Questions