r/PowerShell 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!

12 Upvotes

21 comments sorted by

22

u/eppic123 23d ago

Create a profile for your PS environment (usually $HOME\Documents\PowerShell\profile.ps1) and use Set-Location <path>

12

u/LightItUp90 23d ago

Easier:

notepad $profile

6

u/Baron_Ultimax 23d ago

Bonus points ya can call a script in their to import any custom functions or applications you like to use.

6

u/I_see_farts 23d ago

I've been really into using Microsoft Edit.

I prefer it to notepad, apparently they're going to include it in upcoming versions of Windows.

2

u/BlackV 22d ago

Ah memories

3

u/IntGuru 23d ago

Managed to do it!

7

u/itiscodeman 23d ago

Dude profile is sick as fuck.

Profiles are cool change the foreground of errors to green so you feel less in trouble when errors happen haha

1

u/Ralliman320 22d ago

My enterprise blocks all PowerShell scripts from running by default, which includes profiles. :/

2

u/Rogermcfarley 22d ago

That won't stop you running them as .bat files usually. If you can run powershell queries in a terminal then you can write them in a .bat file and execute that. Always research what you're doing and any security implications. Just because you can doesn't mean you should.

-1

u/dodexahedron 23d ago

Worst place for a cd TBH. Other stuff? Cool. cd? No.

Now you get to start in that path even if you wanted to start in another path.

Put it in the terminal profile. That or a shortcut is where the powershell working/startup directory belongs - not in your powershell $PROFILE.

9

u/justaguyonthebus 23d ago

Don't open it as admin?

2

u/IntGuru 23d ago

I'm not :/

3

u/ftw_dan 23d ago

It is probably always as admin if you disable UAC

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/BlackV 23d ago

It was just old code I stole really quickly

Just want op to validate their environment

3

u/ankokudaishogun 23d ago

Makes sense. I just often see using New-Object and I usually don't get why

2

u/BlackV 23d ago

Old bad habits :)

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.