question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Start-Job needs a -WorkingDirectory parameter

See original GitHub issue

Currently, Start-Job:

  • defaults to different, fixed working directories on different platforms (which is problematic in itself):

    • $HOME on Unix (macOS, Linux)
    • $HOME\Documents on Windows
  • by contrast, using the newly-introduced Unix-like ... & syntax defaults to the current location; this discrepancy is problematic too [update: but as designed] - see #4267

Either way, there is no simple way to have the caller set the working directory explicitly, leading to such painful workarounds as in this SO answer.

The proposed solution:

# Wishful thinking
> $jb = Start-Job -WorkingDirectory $PSHOME { "Hi from $PWD." }; Receive-Job -AutoRemove -Wait $jb
Hi from C:\Program Files\PowerShell\6.0.0-beta.4

Environment data

PowerShell Core v6.0.0-beta.4 on macOS 10.12.5
PowerShell Core v6.0.0-beta.4 on Ubuntu 16.04.2 LTS
PowerShell Core v6.0.0-beta.4 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.413 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
mklement0commented, Oct 15, 2017

I consider -InitializationScript {Set-Location $using:PWD} awkward too.

Having a -WorkingDirectory is a matter of convenience first and foremost, and it also provides symmetry with Start-Process.

You’re running a script block / script somewhere, and it’s helpful to have a simple way to control that somewhere.

This is especially true with the current behavior, where you - invisibly - run in a location other than the current one (unlike when you use the new & operator on Unix - a regrettable discrepancy - see #4267).

As an aside, re implementation detail: Understanding the underpinnings of jobs is important, because users need to be aware that a separate process and remoting are involved to understand that deserialized objects are returned.

2reactions
KirkMunrocommented, Jul 26, 2019

@davinci26 I wouldn’t inject anything into the script block. That could result in some surprises if/when you debug a job (i.e. you should only see your job script when you debug a job, not extra stuff that your job script didn’t include). Instead I’d just set the location when the runspace associated with the job is created, before the script block is run inside of it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell Start-Job Working Directory
A new -WorkingDirectory parameter allows you to specify a directory explicitly. To summarize the problem (applies to Windows PowerShell and ...
Read more >
Start-Job - Module: Microsoft.PowerShell.Core
Start-Job uses the WorkingDirectory parameter to specify the job's working directory. The ScriptBlock parameter uses $PWD to display the job's working directory ......
Read more >
PowerShell cmdLet Start-Job
A job's working directory is set to $home\Documents (Windows Powershell) or $home (PowerShell Core). This is demonstrated by the following simple pipeline: it ......
Read more >
Start-ThreadJob - PowerShell
Start-ThreadJob creates background jobs similar to the Start-Job cmdlet. ... The cmdlet also supports a ThrottleLimit parameter to limit the number of jobs ......
Read more >
PowerShell Start-Job Working Directory
Is there a way to specify a working directory to the Start-Job command? Use-case: I'm in a directory, and I want to open...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found