Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Refresh Environment Variables in PowerShell without Close then Reopen

July 26, 2024
in A, Blog, Powershell
0
ADVERTISEMENT

Table of Contents

Refresh Environment Variables in PowerShell without Reopening

In this post, we will show you how to refresh the Path environment variables without closing and reopening the PowerShell session. For example, we’ve a simple PowerShell script:

  • Install Oy-My-Posh.
  • Wait three seconds.
  • Then install a font for Oh-My-Posh.
winget install JanDeDobbeleer.OhMyPosh -s winget
Start-Sleep -Seconds 3
oh-my-posh font install AnonymousPro

As you can see in the below screenshot. After the app is installed, PowerShell need to reload the Path environment variables to know the path of the executable file (oh-my-posh.exe).

nPXrntfxlRhVvakBLDO9xaEt3l1UyHr9D3boKalHwakmuguRmQ3FU4JX4ez3

PowerShell does not recognize the operable program because it only finds the scripts and executable files in the folders listed in the Path environment variables.

oh-my-posh : The term ‘oh-my-posh’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ oh-my-posh font install AnonymousPro
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (oh-my-posh:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

To fix it, we need to edit out script as follows:

  1. Install Oy-My-Posh.
  2. Wait three seconds.
  3. Update the Path environment variables to include the path of the file oh-my-posh.exe.
  4. Then install a font for Oh-My-Posh.
winget install JanDeDobbeleer.OhMyPosh -s winget
Start-Sleep -Seconds 3

# Update the $env:Path to the current session
$userpath = [System.Environment]::GetEnvironmentVariable("Path","User")
$machinePath = [System.Environment]::GetEnvironmentVariable("Path","Machine")
$env:Path = $userpath + ";" + $machinePath

oh-my-posh font install AnonymousPro

This time, after the path env is updated in the current PowerShell session. PowerShell knows the path of the executable file then using it to install the font with the last command.

NIVjpWlUX4684RZOTXufJAt8jTY3GAHmT4TULfAKiInCLOr5jYaFQsQ2aU6n

Using refreshenv - Chocolatey

Alternatively, if you’re using Chocolatey as your Windows package manager. You can use its function refreshenv to refresh the path environment variables in the current session.

PS C:\> Get-Command refreshenv

Name            : RefreshEnv.cmd
CommandType     : Application
Definition      : C:\ProgramData\chocolatey\bin\RefreshEnv.cmd
Extension       : .cmd
Path            : C:\ProgramData\chocolatey\bin\RefreshEnv.cmd
irm community.chocolatey.org/install.ps1 | iex

Install the Chocolatey in the target computer then you need to add the refreshenv cmdlet into the PowerShell script before installing the font.

winget install JanDeDobbeleer.OhMyPosh -s winget
Start-Sleep -Seconds 3
RefreshEnv
oh-my-posh font install AnonymousPro
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Create a SSH Key Pair for SSH Authentication from Windows PowerShell

Next Post

How To Install or Update OpenSSL 3 on CentOS 8 Linux

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory