How do you set PowerShell's default directory?

前端 未结 13 1713
生来不讨喜
生来不讨喜 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:41

    Step1. open file Microsoft.PowerShell_profile under C:\Users\yourusername\Documents\PowerShell:

    Step2. Add the following line:

    set-location "C:\Whatever\path\you\want\to\set\as\worrkingdir\"

    Step3. relaunch power-shell

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

    Create a PowerShell profile as follows.

    1. Run PowerShell as administrator and execute the following command:

      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

      This will permit PowerShell to run local scripts and scripts downloaded from the Internet that have been signed. Read more about this command in the documentation.

    2. In your Documents folder, find a folder named WindowsPowerShell for classic PowerShell or PowerShell for newer PowerShell Core. If it does not exist, that's ok; just create it.

    3. Create a new file named profile.ps1 in the WindowsPowerShell folder (or PowerShell for PowerShell Core).
    4. Open profile.ps1 and add the following command to set your default working directory:

      Set-Location C:\my\default\working\directory
      
    5. Open a new PowerShell window... the changes should have taken effect.

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

    I had tried the above answers in Windows Server 2016 without success.

    But I found this approach (it should be the same for Windows 10) working for me.

    1. Start a PowerShell session
    2. In the Taskbar, right-click and pin to keep a link there
    3. Again right click the icon in taskbar and then right-click Windows PowerShell and choose Properties
    4. Enter your preferred directory in the Start in: input field and press OK
    5. Start from the taskbar icon

    Done!

    In the same Properties dialog you can also change many other settings like fonts, colors, sizes and on the Shortcut tab there via button Advanced. You can select if that PowerShell session is to be run with administrator privileges.

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

    You could specify the directory to open when starting PowerShell:

    powershell.exe -NoExit -command "& {Set-Location $env:systemroot}"
    

    Just use it in your shortcut.

    Or use a profile to set a start directory.

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

    With that, there seems to be some confusion on the "working directory" and PowerShell's "location". What most people here are doing, and saying to do is change PowerShell's "location". The "working directory" is actually different. Here is an article that explains it.

    For those who don't want to read the article: Open PowerShell and use what others have said to do Set-Location "C:\some\directory". Notice that your "working directory" is still where your PowerShell was opened at. Either "~" or "%SYSTEMROOT%\system32" depending on if you ran as administrator or not. To check the working directory, use [Environment]::CurrentDirectory.

    Note: in the article the author says to check the "working directory" by using this command:

    \[Environment\]::CurrentDirectory
    

    I am not sure if this works with older PowerShell versions, but with PowerShell 5 (and later) you have to use [Environment]::CurrentDirectory.

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

    Type this in PowerShell:

    New-Item -path $profile -type file –force
    

    It creates a .ps1 file in the PowerShell folder. Open it, and edit it as:

    Set-location C:\files
    

    Done

    Refer to this link. It works fine.

    Change PowerShell Start Directory

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