How to revert Powershell PATH variable to the default value - powershell

I have mistakenly replaced the PATH variable instead of appending to it. How do I revert back to the default value of the PATH?

If we want to reset your PATH environment variable without restarting your PowerShell session, give this a try:
$Env:Path = [System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::Machine).Path + ';' + [System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::User).Path

To get the default current path run the following
($env:PATH).split(";")
if i understand your question that you have by mistake replace the path variable so if you run the command on the last answer it will revert back the fault current path it will not add the default which has been missed
The default path part of it is the default for any windows are:
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0
C:\Users$user\AppData\Local\Microsoft\WindowsApps
and the other depending on your apps like that
C:\WINDOWS\System32\OpenSSH
C:\Program Files (x86)\Microsoft Team Foundation Server 2010 Power Tools\Best Practices Analyzer
C:\Program Files\PuTTY
C:\Program Files\Docker\Docker\resources\bin
C:\ProgramData\DockerDesktop\version-bin
C:\Program Files\PowerShell\7-preview\preview
C:\Users$user\AppData\Local\Microsoft\WindowsApps
C:\Users$user\AppData\Local\Programs\Fiddler
C:\Users$user\AppData\Local\Microsoft\WindowsApps
C:\Program Files (x86)\OpenVPN\bin
C:\Users$user\AppData\Local\GitHubDesktop\bin
C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin
C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static
you can search what is the default PowerShell path for each program you have
To add a new path
$INCLUDE = "C:\tmp"
$OLDPATH = [System.Environment]::GetEnvironmentVariable('PATH','machine')
$NEWPATH = "$OLDPATH;$INCLUDE"
[Environment]::SetEnvironmentVariable("PATH", "$NEWPATH", "Machine")

Related

Setting environment variables in powershell

In cmd, you could access cmd environment variables outside of cmd, but is there a way to accomplish that is powershell?
BACKGROUND:
In command-prompt (cmd.exe), there are environment variables like %COMSPEC% and %PROGRAMFILES% that you could use outside of cmd an din file explorer and other places (i. e. shortcuts) just by typing in %VariableName% somewhere (this is how I see a lot of people accessing their appdata folders) but I have yet to find a way to do it in powershell. In cmd, you could even define a variable with setx nd be able to use it outside of cmd. Ex:
setx testbat "%HOMEDRIVE%%HOMEPATH%\Desktop\Programs\test.bat"
Then you could use it like this in shortcuts:
%testbat%
Shortcut Wizard with cmd variable
However, I have not been able to replicate this with Powershell variables.
What I've tried:
I saw this and wondered if making an environment variable in powershell with this in mind could be possible:
$env:test = "C:\Users\Neko"
Then I restarted and tried using this both in cmd and in the search prompt (search prompt)
C:\Users\Neko>cd %test%
The system cannot find the path specified.
C:\Users\Neko>echo %test%
%test%
Sure enough, it didn't even show up in powershell when I did gci env: after the restart
Since this didn't work I deleted the variable and had an idea:
PS C:\Users\Neko> gci env:
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\Neko\AppData\Roaming
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME XXXXXXXXX
ComSpec C:\Windows\system32\cmd.exe
DriverData C:\Windows\System32\Drivers\DriverData
HOMEDRIVE C:
HOMEPATH \Users\Neko
LOCALAPPDATA C:\Users\Neko\AppData\Local
LOGONSERVER \\XXXXXXXXX
NUMBER_OF_PROCESSORS 8
OneDrive C:\Users\Neko\OneDrive
OneDriveConsumer C:\Users\Neko\OneDrive
OS Windows_NT
POWERSHELL_DISTRIBUTION_CHA... MSI:Windows 10 Pro Education
PROCESSOR_ARCHITECTURE AMD64
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 126 Stepping 5, GenuineIntel
PROCESSOR_LEVEL 6
PROCESSOR_REVISION 7e05
ProgramData C:\ProgramData
ProgramFiles C:\Program Files
ProgramFiles(x86) C:\Program Files (x86)
ProgramW6432 C:\Program Files
PSModulePath C:\Users\Neko\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\M...
PUBLIC C:\Users\Public
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\Neko\AppData\Local\Temp
test C:\Users\Neko
TMP C:\Users\Neko\AppData\Local\Temp
USERDOMAIN XXXXXXXXX
USERDOMAIN_ROAMINGPROFILE XXXXXXXXX
USERNAME Neko
USERPROFILE C:\Users\Neko
windir C:\Windows
WSLENV WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID XXXXXXXXX
WT_SESSION XXXXXXXXX
PS C:\Users\Neko> del "env:test"
PS C:\Users\Neko> gci env:
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\Neko\AppData\Roaming
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME XXXXXXXXX
ComSpec C:\Windows\system32\cmd.exe
DriverData C:\Windows\System32\Drivers\DriverData
HOMEDRIVE C:
HOMEPATH \Users\Neko
LOCALAPPDATA C:\Users\Neko\AppData\Local
LOGONSERVER \\XXXXXXXXX
NUMBER_OF_PROCESSORS 8
OneDrive C:\Users\Neko\OneDrive
OneDriveConsumer C:\Users\Neko\OneDrive
OS Windows_NT
Path C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPo...
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
POWERSHELL_DISTRIBUTION_CHA... MSI:Windows 10 Pro Education
PROCESSOR_ARCHITECTURE AMD64
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 126 Stepping 5, GenuineIntel
PROCESSOR_LEVEL 6
PROCESSOR_REVISION 7e05
ProgramData C:\ProgramData
ProgramFiles C:\Program Files
ProgramFiles(x86) C:\Program Files (x86)
ProgramW6432 C:\Program Files
PSModulePath C:\Users\Neko\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\M...
PUBLIC C:\Users\Public
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\Neko\AppData\Local\Temp
TMP C:\Users\Neko\AppData\Local\Temp
USERDOMAIN XXXXXXXXX
USERDOMAIN_ROAMINGPROFILE XXXXXXXXX
USERNAME Neko
USERPROFILE C:\Users\Neko
windir C:\Windows
WSLENV WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID XXXXXXXXX
WT_SESSION XXXXXXXXX
I then checked to see if the cmd set command beared the same results as gci env: and it did, so I then tried to use setx to see if it appeared in powershell as well:
setx test "Test"
And after a restart, it did
PS C:\Users\Neko> $env:test
Test
I realized that the variables all were stored in the registry and that I could edit the registry of course with:
Set-Itemproperty -path 'HKCU:\Environment' -Name 'Test' -value 'Test'
But it doesn't feel the same as something like setx
CONCLUSION:
I want to learn if there is a true-powershell-esque command that can define environemnt variables that I can use outside of powershell like setx can in cmd. I am not looking for full scripts just commands or something like $env:variable = "value".
UPDATE:
Yes, you can create a function to do this as well, technically a one-liner possibly, not what I'm looking for. I want to be able to create environment variables in powershell with built in cmdlets. Something "true-powershell"
Powershell itself provides these methods to interact with environment variables:
The Environment provider drive
The Item cmdlets
The .NET System.Environment class
On Windows, the System Control Panel
So, to set a persistent environment variable (PSModulePath) to a value $newpath at machine level you might use
[Environment]::SetEnvironmentVariable("PSModulePath", $newpath, 'Machine')
To get an environment variable you might use
$path = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
or the variable syntax
$Env:PSModulePath
Here's the documentation.

Changing a value in the Path environment variable

My company uses a program that breaks when Java is updated. This is due to the program install (I assume) placing a static path to Java in the Path environment variable. For example, the current Path variable in question is C:\Program Files (x86)\Java\jre1.8.0_171\bin\client, but if Java is updated and the program is re-installed, the Path variable will update to include C:\Program Files (x86)\Java\jre1.8.0_181\bin\client.
I was able to find exactly what I needed (I think) here https://blogs.technet.microsoft.com/heyscriptingguy/2007/11/08/hey-scripting-guy-how-can-i-remove-a-value-from-the-path-environment-variable/, but that code is for Powershell 2.0 and doesn't work on Windows 10.
Is this still possible in Windows 10?
You can use the System.Environment class to modify your environment variables machine-wide:
# get the PATH and split it up
$PATH = [Environment]::GetEnvironmentVariable('PATH', 'Machine') -split ';'
# filter out the JRE paths
$PATH = $PATH -notmatch 'java\\jre'
# get any real JRE paths
$PATH += (Get-Item -Path "${Env:ProgramFiles(x86)}\Java\jre*\bin\client").FullName
$PATH = $PATH -join ';'
[Environment]::SetEnvironmentVariable('PATH', $PATH, 'Machine')
Note: You will need to run your shell elevated to execute these commands.
Java is not a Windows issue, and therefore TechNet will not help you. There is a fix below.
Windows 10 and Windows 8:
Open the Legacy Control Panel
Click the Advanced system settings link.
Click Environment Variables. In the section System Variables, find the PATH environment variable and select it.
Click Edit.
If the PATH environment variable does not exist, click New.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable.
Click OK.
Close all remaining windows by clicking OK.
Reopen the PowerShell window, and run your Java code.
Source: https://www.java.com/en/download/help/path.xml

Powershell add to path, temporarily

I'm trying to write a simple PowerShell script to deploy a Visual Studio ASPNET Core 1 project.
Currently, in a batch file i can say
Path=.\node_modules\.bin;%AppData%\npm;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External;%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External\git
That will modify the path variable for the duration of the session... for the life of me I can't figure out how to do this simple thing in powershell.
Can someone help me translate this into powershell?
TIA!
Option 1: Modify the $env:Path Variable
Append to the Path variable in the current window:
$env:Path += ";C:\New directory 1;C:\New directory 2"
Prefix the Path variable in the current window:
$env:Path = "C:\New directory 1;C:\New directory 2;" + $env:Path
Replace the Path variable in the current window (use with caution!):
$env:Path = "C:\New directory 1;C:\New directory 2"
Option 2: Use the editenv Utility
I wrote a Windows command-line tool called editenv that lets you interactively edit the content of an environment variable. It works from a Windows console (notably, it does not work from the PowerShell ISE):
editenv Path
This can be useful when you want to edit and rearrange the directories in your Path in a more interactive fashion, and it affects only the current window.
You can just use $env:Path, but if you're anxious that that isn't explicit enough, you can use System.Environment.SetEnvironmentVariable():
[System.Environment]::SetEnvironmentVariable('Path',$Value,[System.EnvironmentVariableTarget]::Process);
And GetEnvironmentVariable() can explicitly retrieve:
[System.Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::Process);
[System.Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::Machine);
[System.Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::User);

How to properly set path in Powershell and 7zip?

I have a Powershell script to create a self-extracting archive via 7zip. But it's receiving this error:
cannot find specified SFX module
The Powershell code is:
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe
sz a -t7z -sfx -ppassword $fullpath $filetostore
Both variables are valid. I've tried -sfx and -sfx7z.sfx, same error. The 7z.sfx file is indeed in the correct folder with 7zip. I can also verify the alias is working, as the 7zip copyright appears when running the code (so 7zip commandline is being initiated). This command works outside Powershell.
I'm also tried Set-Location into the 7zip folder, but same error. What am I missing?
It seems you should add the 7-zip folder to your PATH environment variable to make things easier :
#find the 7-zip folder path
$7zPath = (Get-ChildItem "C:\Program Files","C:\Program Files (x86)" -Include "7-zip" -Recurse -ErrorAction SilentlyContinue).FullName
#add it to PATH environment variable
$env:Path += ";$7zPath;"
Then you can run 7z -sfx with no errors about the SFX module.
While Sodawillow has an answer that will work for the active session, a more permanent answer would be to add 7zip to the path for the Environment you are working in:
[Environment]::SetEnvironmentVariable("Path",$env:Path+";C:\Program Files\7-zip", [EnvironmentVariableTarget]::User)
The above one-liner should add 7zip to the active user account's path. Change 'User' to 'Machine' for the whole computer, or 'Process' for the currently running window. If you set 'User' or 'Machine', you will need to open a new powershell instance to see the change reflected.

The term 'xsd' is not recognized as the name of a cmdlet, function, script file, or operable program

When executing the following script (this is a part of the actual script), I'm getting the following error message in powershell:
The term 'xsd' 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:10 char:3
$xsds = ls *.xsd | %{ $_.Name }
if ($xsds.Count -eq 0) { exit }
# Add '.\' to last file name, see http://stackoverflow.com/questions/906093/xsd-exe-output-filename
$last = $xsds | select -last 1
$last = '.\' + $last
$xsds[$xsds.Count - 1] = $last
& xsd $xsds /c /n:OutputFolder
Are there some requirements for Powershell that I need to install to be able to run the 'xsd' cmdlet first?
The output of $env:Path:
PS C:\Users\Administrator\Desktop\New> $env:Path
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Mi
crosoft\Web Platform Installer\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files
(x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Micro
soft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program F
iles (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\
PS C:\Users\Administrator\Desktop\New>
An xsd.exe is available in the folders:
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64
The paths you have listed are not part of your PATH environment variable. So that leaves you with two options. Add the directories to path or just reference the exe by its full path.
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\xsd.exe" $xsds /c /n:OutputFolder
If you want to change your paths you could update them like this
$env:Path += ";C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools"
If you need x64 paths just update the strings.