1

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?

user2909630
  • 13
  • 1
  • 3

1 Answers1

4

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
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Thanks. That worked. This is broken behavior in my view. http://stackoverflow.com/questions/2224350/powershell-start-job-working-directory – user2909630 Oct 23 '13 at 03:33
  • I don't know about broken but it would be nice if `Start-Job` had a `WorkingDirectory` parameter. – Keith Hill Oct 23 '13 at 03:48