How do you set PowerShell's default directory?

前端 未结 13 1714
生来不讨喜
生来不讨喜 2021-01-30 01:37

Is there a way to change PowerShell\'s default location?

How do you set PowerShell\'s default working directory?

相关标签:
13条回答
  • 2021-01-30 01:55

    Instead of unconditionally changing the working directory as mentioned in previous answers, you can write a simple function in the PowerShell profile to use Set-Location to quickly change the working directory whenever necessary.

    Check Jeremy Danyow's answer to create/modify a PowerShell profile.

    Add a function(s) to your PowerShell profile:

    function goto_this {set-location 'your\path\to\some\dir'}
    function goto_that {set-location 'your\path to some\dir with space'}
    

    Just change the function name and directory pointed to. Using quotes on the path is mandatory if it contains spaces. I try to keep the prefix goto_ as it helps in remembering the functions' names.

    You can start typing goto_ then press TAB to cycle through all the added functions (remember to start a new PowerShell window after adding/modifying functions).

    0 讨论(0)
  • 2021-01-30 01:56

    An easier way to set the default directory is the following:

    1. Right click the Windows PowerShell icon and pin to Start
    2. Right click the Windows PowerShell icon in Start, and again right click Windows PowerShell and select Properties (not Run as Administrator and not Windows PowerShell ISE)

      1. In the Shortcut tab -> 'Start in' field, change to the location you want PowerShell to start in.

    0 讨论(0)
  • 2021-01-30 01:57

    Write-Output "Set-Location C:\" >> $profile

    0 讨论(0)
  • 2021-01-30 01:59

    Putting Set-Location into your profile will unconditionally change the current working directory, which might have unwanted consequences in regards to the working directory for scripts that you execute via "run with PowerShell".

    An alternative solution is to change the working directory for the .lnk files to PowerShell usually found in %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell. Right click on a link, and change the working directory from %HOMEDRIVE%%HOMEPATH% to the directory you want.

    0 讨论(0)
  • 2021-01-30 02:03

    Using just the command line, if a file exists already it will append to it:

    $(if (-Not (Test-Path ~\Documents\WindowsPowerShell\)){ mkdir ~\Documents\WindowsPowerShell\}) ; echo "Set-Location c:\THELOCATIONYOUWANT" >> ~\Documents\WindowsPowerShell\profile.ps1
    
    0 讨论(0)
  • 2021-01-30 02:06

    Make this the first line in your Profile.ps1 and PowerShell Core (pwsh) will open in the directory you are currently working in:

    set-location (get-location).path

    0 讨论(0)
提交回复
热议问题