Adding Windows PowerShell Environment Variables with Profile.ps1 File

Add Environment Variable using Profile

If you are using PowerShell in Windows, there is a high chance that you might need to add execution file to your environment variable. Well, there are many doing this, I will show you how to add a Profile.ps1 file so you make the environment variable permanent. Here are steps

1. Open your PowerShell

I assume you know how to do that

2. Check the profile path

Type the following command in the PowerShell

PS C:\> $profile
C:\Users\panda\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

3. Create the file if it doesn’t exists

PS C:\> notepad C:\Users\panda\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

4. Add the variable you need, for example, adding a node folder

$env:Path += ";D:\Bin\node"

5. Exit and Start a new PowerShell, you can check the path using the command in PowerShell below

PS C:\> $env:path

Additional Steps if error occur when opening PowerShell

If the following error occur, it mean that the scripts is being disabled, to enable it, follow the steps below

. : File C:\Users\panda\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts is disabled
on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . 'C:\Users\panda\Documents\WindowsPowerShell\profile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

1. Check the policy list

PS C:\Users\panda> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined

2. Update the policy

PS C:\Users\panda> Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y

3. Verify it

PS C:\Users\panda> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    Unrestricted
 LocalMachine       Undefined

4. Restart PowerShell and check the path variable

PS C:\> $env:path

Reference

Leave a Comment