I tried but did not find an answer :
How do I get the current user home in Windows PowerShell?
System variables (those that you would address in Batch as %varname%) are accessible in PowerShell as $env:varname. You can list the system variables that are visible to your PowerShell session with Get-ChildItem -Path Env:.
In Windows, it looks like $env:userprofile or $env:homepath without the drive.
dir env: | where value -match admin
Name Value
---- -----
APPDATA C:\Users\admin\AppData\Roaming
HOMEPATH \Users\admin
LOCALAPPDATA C:\Users\admin\AppData\Local
OneDrive C:\Users\admin\OneDrive
Path C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Wind...
PSModulePath C:\Users\admin\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerSh...
TEMP C:\Users\admin\AppData\Local\Temp
TMP C:\Users\admin\AppData\Local\Temp
USERNAME admin
USERPROFILE C:\Users\admin
Try this:
[Environment]::ExpandEnvironmentVariables("%UserProfile%")
Related
For to display all environmental variables in PowerShell one uses:
Get-ChildItem env:
What is 'env:'?
It clear to me that it is some abbreviation for "environment". But what kind of abbreviation? What's the meaning of the colon at the end?
Your query is defined in the PowerShell help files:
About Automatic Variables:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.1
$env is a virtual drive for the above. Easily seen via the below and its purpose to allow access to the above:
# These are treated as normal filesystem drives, and you can create custom ones.
Get-PSDrive
<#
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Alias Alias
C 5.04 34.83 FileSystem C:\ Scripts
Cert Certificate \
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
Variable Variable
WSMan WSMan
#>
Get-ChildItem -Path 'env:\'
<#
Using these in code requires the $ in front of each named variable to get the content/values. In your scripts, never name your custom variables the same as any of the below.
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\WDAGUtilityAccount\AppData\Roaming
CLIENTNAME 8fda9520-99a6-4
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME 0C092C31-6890-4
...
#>
If you do want to use the names above, as custom variables for other values, then you should provide a unique prefix. Say, your initials, so as to not have conflicts/errors.
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")
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.
I wanted to ask how one would check who has access to subfolders in a certain directory on a server using either the CMD or Powershell?
For NTFS permissions I like to use the NTFSSecurity PowerShell Module as the output is similar to the windows permissions GUI.
It has simple commands for adding and removing permissions, which is an ugly process using the standard acls commands!
To see current NTFS permissions using this module:
Get-NTFSAccess -Path "\\server\share\folder"
Which would give an output like this:
You are looking for icacls. From cmd type icacls directoryname /t replacing directoryname with the actually directory name to display all of the access permissions for the directory and subdirectories. The /t flag specifies to look in subdirectories. For more info just type in icacls into cmd or look at this link: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/icacls
In our corporate environment, I am having difficulty with creating a PowerShell profile scripts.
To prevent users from writing documents on the local disk, the "Documents" directory is forced to be on a network drive. Commonly the "H:" (home) drive.
Likewise, users are forbidden from writing under C:\Windows\System32.
Where can I put the ISE profile script if these two are not available?
PSVersion 5.0.10586.117
PS C:\Windows\System32\WindowsPowerShell\v1.0> $HOME, $PSHOME
C:\Users\pwatson
C:\Windows\System32\WindowsPowerShell\v1.0
See also: Help-About about_Profiles
When I am not connected to the network, these are the $profile settings. I still cannot write under C:\Windows\System32 and the CurrentUser values are invalid.
PS C:\Windows\System32\WindowsPowerShell\v1.0> $profile | Get-Member -Type NoteProperty | ForEach-Object {$_.ToString
()}
string AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
string AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
string CurrentUserAllHosts=WindowsPowerShell\profile.ps1
string CurrentUserCurrentHost=WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PS C:\Windows\System32\WindowsPowerShell\v1.0>
One option is to create a shortcut with a target like this:
%systemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -file C:\somewhere\myprofile.ps1
and then always use this shortcut to start PowerShell.
This is not actually using the built-in PowerShell profile concept but it is dot-sourcing a ps1 file that behaves pretty much like a profile file.
If you like to start PowerShell from cmd.exe, create a batch-file with the same content as above and put it somewhere in your path (if you have permissions to do so)