PowerShell Studio 2022 $Profile Cannot take effect

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
iaotoman
Posts: 6
Last visit: Mon Oct 03, 2022 3:40 pm
Has voted: 1 time

PowerShell Studio 2022 $Profile Cannot take effect

Post by iaotoman »

hello everyone,

I just newly installed PSS version 5.8.202
But the configuration file $profile didn't work. I recorded GIF animation, Please see below
I have restarted the computer and refreshed the cache

In addition, I tried several other $profile files, but they didn't take effect,
I need help. Thank you in advance

Product, version and build: pss Version 5.8.202
Operating system: window7 64bit
PowerShell version(s): 5.1
32-bit version of software? no
aaaa.gif
aaaa.gif (484.03 KiB) Viewed 2640 times
User avatar
Alexander Riedel
Posts: 8472
Last visit: Mon Mar 18, 2024 2:59 pm
Answers: 19
Been upvoted: 37 times

Re: PowerShell Studio 2022 $Profile Cannot take effect

Post by Alexander Riedel »

Profiles are tools to pre-configure your local console to your liking and your needs. They are somewhat useless for script development. Now, please don't start to be offended. I know there are exceptions.

But generally when you write a script or, as with PowerShell Studio, create an application, you are developing code that has to run in your environment for a long time, otherwise you wouldn't bother to write a script. Likewise, you are very likely to develop something that is used on a wide variety of machines in your environment, maybe even in multiple locations globally.

Therefore, having the application or even a simple script utterly depend on your local development machine's settings is basically gearing you up for a lot of frustration.
As soon as you publish that script you will get feedback that it doesn't work, or behaves strangely and you will utter the famous words every developer knows oh so well:

"But it works on my machine."

Even worse, if your corporate machines are configured to have absolute identical profiles, it may all work fine. But one day the new intern will tweak the profile to make it "better" and roll that out.
The you get to the second very common sentence:

"But it worked yesterday."

From experience, that second scenario will hit at the most inopportune time possible. While you are on honeymoon, packing for your vacation starting tomorrow, your sister's wedding etc. :D

You don't want to do that. If for some reason you really, really need a profile loaded, just dot source your desired profile as the first statement in your script. I would encourage you to consider including the code you need in your actual script though. PowerShell Studio does not load any profiles when you run or debug a script. Likewise, it does not load profiles in any packaged executable. This is to alert you very early to any potential dependencies on local setting you want to avoid.

So why does PowerShell Studio have functionality to manage profiles?
Simple, for testing. As you test your application your should run it in a console, with maybe a different set of profiles your application may encounter in the wild. This way you can detect possible side effects early on. For example, your profile loads one version of a module and your application needs a newer version. It may try to load it but the older version is already there. Who wins?

Please let us know if you have any further questions on this.
Alexander Riedel
SAPIEN Technologies, Inc.
iaotoman
Posts: 6
Last visit: Mon Oct 03, 2022 3:40 pm
Has voted: 1 time

Re: PowerShell Studio 2022 $Profile Cannot take effect

Post by iaotoman »

Alexander Riedel wrote: Sat Mar 26, 2022 12:51 am Profiles are tools to pre-configure your local console to your liking and your needs. They are somewhat useless for script development. Now, please don't start to be offended. I know there are exceptions.

But generally when you write a script or, as with PowerShell Studio, create an application, you are developing code that has to run in your environment for a long time, otherwise you wouldn't bother to write a script. Likewise, you are very likely to develop something that is used on a wide variety of machines in your environment, maybe even in multiple locations globally.

Therefore, having the application or even a simple script utterly depend on your local development machine's settings is basically gearing you up for a lot of frustration.
As soon as you publish that script you will get feedback that it doesn't work, or behaves strangely and you will utter the famous words every developer knows oh so well:

"But it works on my machine."

Even worse, if your corporate machines are configured to have absolute identical profiles, it may all work fine. But one day the new intern will tweak the profile to make it "better" and roll that out.
The you get to the second very common sentence:

"But it worked yesterday."

From experience, that second scenario will hit at the most inopportune time possible. While you are on honeymoon, packing for your vacation starting tomorrow, your sister's wedding etc. :D

You don't want to do that. If for some reason you really, really need a profile loaded, just dot source your desired profile as the first statement in your script. I would encourage you to consider including the code you need in your actual script though. PowerShell Studio does not load any profiles when you run or debug a script. Likewise, it does not load profiles in any packaged executable. This is to alert you very early to any potential dependencies on local setting you want to avoid.

So why does PowerShell Studio have functionality to manage profiles?
Simple, for testing. As you test your application your should run it in a console, with maybe a different set of profiles your application may encounter in the wild. This way you can detect possible side effects early on. For example, your profile loads one version of a module and your application needs a newer version. It may try to load it but the older version is already there. Who wins?

Please let us know if you have any further questions on this.
Thank you for your help. I use the following code at the first line of the code, but it still doesn't work
. "$PROFILE"

If there is an option to enable the $profile function,it would be great. I really need this function

For variables related to $profile, their values cannot be displayed in the editor
User avatar
Alexander Riedel
Posts: 8472
Last visit: Mon Mar 18, 2024 2:59 pm
Answers: 19
Been upvoted: 37 times

Re: PowerShell Studio 2022 $Profile Cannot take effect

Post by Alexander Riedel »

[Topic moved by moderator]

This seems to be more of a PowerShell question than product related.
$PowerShell is a variable that contains the paths to the loaded profiles. So, as none have been loaded it is empty. You cannot use that this way.
You need to dot source the actual powershell file you want to load.

I guess to take this further it would be good to know what it is about your profile that makes it indispensable.
Alexander Riedel
SAPIEN Technologies, Inc.
iaotoman
Posts: 6
Last visit: Mon Oct 03, 2022 3:40 pm
Has voted: 1 time

Re: PowerShell Studio 2022 $Profile Cannot take effect

Post by iaotoman »

Alexander Riedel wrote: Sat Mar 26, 2022 12:05 pm [Topic moved by moderator]

This seems to be more of a PowerShell question than product related.
$PowerShell is a variable that contains the paths to the loaded profiles. So, as none have been loaded it is empty. You cannot use that this way.
You need to dot source the actual powershell file you want to load.

I guess to take this further it would be good to know what it is about your profile that makes it indispensable.
Thank you again for your reply
Many times, I need to test the code locally. It's not convenient to add reference files in front of all the code

The $profile or $global:PROFILE variable does not have any display. It can be displayed in ISE. Please see the picture
cont.gif
cont.gif (286.27 KiB) Viewed 2487 times
-----------------------------------------------------------------------------------------------------
In addition, how to clone the current line of code? In other code editors, the hotkey is Ctrl + D. can I customize the hotkey?
-----------------------------------------------------------------------------------------------------
gcb is the alias of get-clipboard, scb is the alias of set-clipboard, but it is not identified by color in the editor, Please see the picture
8.png
8.png (8.95 KiB) Viewed 2484 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell Studio 2022 $Profile Cannot take effect

Post by jvierra »

This is how to get the user profile and dot-source it.
  1. $userprofile = Join-path  ([environment]::GetFolderPath('MyDocuments')) 'WIndowsPowerShell\Microsoft.PowerShell_profile.ps1'
  2. . $userprofile
iaotoman
Posts: 6
Last visit: Mon Oct 03, 2022 3:40 pm
Has voted: 1 time

Re: PowerShell Studio 2022 $Profile Cannot take effect

Post by iaotoman »

jvierra wrote: Sat Mar 26, 2022 5:49 pm This is how to get the user profile and dot-source it.
  1. $userprofile = Join-path  ([environment]::GetFolderPath('MyDocuments')) 'WIndowsPowerShell\Microsoft.PowerShell_profile.ps1'
  2. . $userprofile
https://docs.microsoft.com/en-us/powers ... e-variable
Above is Microsoft's help page
In PowerShell ise, the following variables can be used, but they cannot take effect in PSS
--------------------------------------------------------------------------------------------------------------------------
Current User, Current Host --- $PROFILE
Current User, Current Host --- $PROFILE.CurrentUserCurrentHost
Current User, All Hosts --- $PROFILE.CurrentUserAllHosts
All Users, Current Host --- $PROFILE.AllUsersCurrentHost
All Users, All Hosts --- $PROFILE.AllUsersAllHosts
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell Studio 2022 $Profile Cannot take effect

Post by jvierra »

That is correct. PSS does not allow access to the "$profile" object. You have to load any profile you want in code.

If PS is installed at 5.x on Windows 10, 11 then the method I posted will load the current user's profile. That is what you seem to have stated that you needed.

All of the profiles are in specific locations. I suspect you also be using profiles incorrectly which is usually how we get to this point.

All profiles are specific to "PowerShell.exe". They are designed to either support machine global variables or local user "convenience" variables. Profiles should not be used as a database. If you have local user data, then it should be in a data file (XML is usually the best type) to store user data for sharing amongst multiple processes.

Also, I want to clarify something. PowerShell is not an application language; it is an automation scripting system. The PowerShell Team made it clear that PowerShell would not support applications. When a script is packaged in any version of the PowerShell "host" then the profiles are not loaded. This is part of the overall PowerShell design.

Also, as Alex noted, scripts that are to be deployed should never be designed with any external dependencies that cannot be resolved at the time the script is deployed. This is also true of any application that is a true application built with a compiled language.

Scripting environments are designed for automation of management functions. When we try to go beyond that then things start to fall apart.

I do sympathize because I have run into similar roadblocks because the companies, I consulted for set things up poorly and, due to conflicting dependencies, we could not sort out the mess. At time this meant abandoning the project or undergoing a time-consuming redesign. I know the frustration in that from many experiences.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell Studio 2022 $Profile Cannot take effect

Post by jvierra »

As far as I know the PowerShell ISE is no longer supported by Microsoft. PowerShell script development has been transferred to VSCode which is supported as OpenSource.
This topic is 1 year and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked