Skip to content

When using Workingdirectory parameter on pwsh, working directory gets set AFTER execution of $PROFILE script #7895

Closed

Description

The default shortcuts for pwsh.exe on Windows are calling it using -WorkingDirectory ~ by default, which is correct and by design.
However, if I wanted to temporarily change/set the final working directory, then the most intuitive way for most users (not familiar with this new feature) would be to add code like cd C:\foo into the $PROFILE script.
Unfortunately, only AFTER executing the script in $PROFILE, the working directory gets set to the one specified by the WorkingDirectory parameter. If it got set BEFORE executing the $PROFILE script then this problem would not exist and the working directory when $PROFILE executes would be a more reasonable one as well.

Steps to reproduce

Put the following code into $profile

pwd
cd C:\

Then launch pwsh using the -WorkingDirectory parameter

Expected behavior

The printed working directory when executing the script in $PROFILE should be the one specified by -WorkingDirectory of pwsh, therefore the $PROFILE should be able to override and change that working directory

Actual behavior

The working directory when the $PROFILE script executes is C:\Windows\System32 instead of the path specified by the -WorkingDirectory parameter on pwsh, the final working directory is not C:\ as it is being set inside the script.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    WG-Engine
    Issue-Discussion
    on Sep 29, 2018
  2. DTW-DanWard commented on Oct 11, 2018

    @DTW-DanWard

    This has been driving me nuts the past few days. Maybe a quick solution is to not add the WorkingDirectory parameter to the shortcuts...? Developers/desktop users are always going to set the default location via profile, not by changing the shortcut value. WorkingDirectory seems only useful for automating a script or running one remotely. But, to be honest, even then I wouldn't rely upon it; if my script had to be in a default location at start-up I would ensure that at the beginning of the script itself and not trust that the shell had been opened to the correct default location.

  3. bergmeister commented on Oct 11, 2018

    @bergmeister
    ContributorAuthor

    We should rather fix this properly in the implementation of the new Workingdirectory parameter that was introduced in PR #6612 to help start PowerShell by default in the user folder instead of its installation folder and for an issue with the explorer context menu. cc Steve Lee (@SteveL-MSFT)
    The proposed design would be to have the WorkingDirectory ready before execution of the $PROFILE script instead afterwards.

  4. RichardSiddaway commented on Oct 18, 2018

    @RichardSiddaway

    If the working directory is going to be set the user should be given the option of overriding the default when PowerShell is installed. we get the option to override the install folder why not the workingdirectory?

  5. bergmeister commented on Oct 18, 2018

    @bergmeister
    ContributorAuthor

    RichardSiddaway We could certainly add an option to specify a different working directory for the start menu shortcut (or nothing at all, pwsh would then start in the installation folder) but this would not address the problem highlighted by this issue where the working directory gets set too late.
    The reason why the user can choose the installation folder is just because it is part of the basic WiX template that provides this functionality out of the box for free.

  6. RichardSiddaway commented on Oct 18, 2018

    @RichardSiddaway

    It may not solve the conflict between the profile and the -workingdirectory parameter but it would effectively remove the conflict or the need to set a directory in your profile. It would also be a potential (partial) solution to issue #8069

  7. SteveL-MSFT commented on Oct 19, 2018

    @SteveL-MSFT
    Member

    I think for the majority of users, having the -WorkingDirectory ~ in the shortcut is the preferred experience to have it default to their home directory than $PSHOME. The fix for this immediate problem is simple, just move the processing of -WorkingDirectory earlier although it could have a breaking change impact if $PROFILE expects to start in $PSHOME.

  8. added
    Resolution-Won't Fix
    on Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-DiscussionResolution-Won't FixWG-Engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions