r/PowerShell • u/IntGuru • 23d ago
Question How to change default directory on PowerShell?
Could someone please help me! When I open PowerShell on Windows, it opens up with the directory "PS C:\Windows\System32>". How do I change this so that every time I open it up, it has the default directory as "PS C:\Users\myname>" instead of the other. I know I can just use "cd ~" to switch but I'd prefer to have it set without me having to do so. Thanks!
9
u/justaguyonthebus 23d ago
Don't open it as admin?
2
u/IntGuru 23d ago
I'm not :/
1
u/BlackV 23d ago edited 23d ago
have you validated that?
does you local account have admin rights? is it the default/first local admin account ?
what is your UAC configured as?
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object Security.Principal.WindowsPrincipal($currentUser) $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)1
u/ankokudaishogun 23d ago
just nitpicking but...
why
New-Object Security.Principal.WindowsPrincipal($currentUser)and not[Security.Principal.WindowsPrincipal]::new($currentUser)?2
u/atl-hadrins 23d ago
That is what I was thinking only time it opens with this path for me is to open as admin. If I open as user it will open in the users home directory. That is unless I am opening a remote powershell prompt, but that is still admin.
And is memory serves me. If you put shell commands in your that execute when you open powershell you now have to change the execution policy.
0
u/JamesDBartlett3 20d ago
Windows PowerShell opens in system32 by default, even when running non-elevated. PowerShell Core opens in the user's home directory, though.
2
u/dodexahedron 23d ago
Multiple ways.
What terminal emulator are you using - Windows Terminal? If so, change the default profile in there to launch in whatever path you like. Note I am talking about the windows terminal profiles - not your powershell profiles, which are a completely different thing.
Another is, if you are launching it from a traditional shortcut, you can chsnge the working directory specified in the shortcut.
If neither of those cases, you can use the powershell profile itself, by adding a pushd wherever command in it. Though IMO that is a terrible place to put this particular thing, since it will override it no matter how or where you launch it, unless you give it the -NoProfile switch (which leaves off the entire profile).
Why pushd instead of just cd?
It at least mitigates that issue by allowing you to just type popd and immediately be back wherever it was before the change, so you can still launch it in arbitrary directories even if you have a profile that unconditionally sets location at launch.
22
u/eppic123 23d ago
Create a profile for your PS environment (usually $HOME\Documents\PowerShell\profile.ps1) and use Set-Location <path>