1

I would like to configure PowerShell to run the default profile.ps1 script from a local folder, instead of my configured Documents folder.

I initially configured my Document Folder to be saved inside my OnDrive to prevent losing my Documents in case of a crash. However, due to that change, when I launch Powershell, it is trying to execute the profile.ps1 script from the preconfigured OneDrive/Documents.profile.ps1 and that is not allowed for security reasons.

I would like to configure Powershell to run the profile.ps1 from a local folder on my C: instead. How do I proceed?

I Opened PowerShell and ran the $PROFILE command. It displayed the OneDrive Documents folder location.

C:\Users\Joey\OneDrive\Documents\WindowsPowerShell\profile.ps1

I think I can point the $PROFILE variable to the local C: folder, but can't seem to find its location to update it. I Tried adding a PROFILE variable to the System Environment Variable, but that did not work.

Please advise.

Johnny
  • 819
  • 1
  • 10
  • 24
  • A guide to the answers: The question asks about _selectively_ changing the location that `$PROFILE` points to: This _isn't supported_ (as of this writing, but it's unlikely to change). You can _indirectly_ change what `$PROFILE` points to, by _fundamentally changing the location of the well-known Documents folder_, which, of course, is a much more fundamental change. – mklement0 Jun 08 '23 at 01:41

2 Answers2

1

You can find a detailed explanation here (in fact multiple) - https://serverfault.com/questions/195397/change-the-powershell-profile-directory - but the short version is, you can adjust that by editing the Personal key within the registry here :

Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

So it no longer points to %USERPROFILES%\Documents, and instead points somewhere that works for your purposes.

Keith Langmead
  • 785
  • 1
  • 5
  • 16
  • 1
    Thank you for your link reference. I use the Powershell command to Edit the Registry to point it to my local C: folder and it works just fine when I call Command as well as when I Open a new Terminal in Visual Studio Code.: New-ItemProperty 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' Personal -Value 'Your New Path Here' -Type ExpandString -Force – Johnny Jun 07 '23 at 16:01
  • It's worth noting that this only _indirectly_ changes the `$PROFILE` location, namely by changing the location of the user's Documents folder, i.e. changes the location of _all documents_. The registry approach is [discouraged](https://devblogs.microsoft.com/oldnewthing/20031103-00/?p=41973) and _not officially supported_, but still seems to work in practice. [This answer](https://stackoverflow.com/a/25709702/45375) shows an (untested by me) solution that uses the _supported_ approach via P/Invoking the `SHSetKnownFolderPath` WinAPI function. – mklement0 Jun 08 '23 at 01:20
1

Note: If changing your entire Documents folder back to a local-only location is an option (away from OneDrive), see Keith Langmead's answer.


The value(s) of the automatic $PROFILE variable can not be changed, and given that dot-sourcing a different file from the profile file in the fixed location is not an option in your case, you'll have to use the PowerShell CLI with its -File parameter to emulate profile loading[1]

powershell.exe -NoExit -File C:\path\to\your\local\profile.ps1

Note: If you add -NoProfile before -File, then none of the - potentially multiple - profile files will be loaded. If there are no other profiles, you can use -NoProfile; otherwise, just delete / don't create one in the location that $PROFILE points to.

You can create a shortcut file (*.lnk) or a batch file (*.cmd) to facilitate launching your PowerShell sessions with this command line.


[1] -File implicitly dot-sources the specified script file, i.e. executes it directly in the global scope, which is also how profiles are loaded.

mklement0
  • 382,024
  • 64
  • 607
  • 775