Start-Job needs a -WorkingDirectory parameter
See original GitHub issueCurrently, 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:
- Created 6 years ago
- Reactions:10
- Comments:9 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 withStart-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.
@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.