I am new to PowerShell, so please forgive the newbie question.
What specific syntax must be changed in the command below in order to have PowerShell create the mysubdirectory
in the $USER_HOME
directory regardless of what that directory is named on any given machine?
The command we are currently using in GitHub runners is brittle because it explicitly types the GitHub runner's user home as follows:
New-Item -ItemType Directory -Force -Path C:\\Users\\runneradmin\\mysubdirectory\\
This OP is asking how to modify the above one-line command to express $USER_HOME
as a variable instead of as the brittle string C:\\Users\\runneradmin
.
CodePudding user response:
PowerShell's automatic $HOME
variable reflects the current user's home (profile) directory, which on Windows is equivalent to $env:USERPROFILE
(the USERPROFILE
environment variable)[1] - in spite of what the linked help topic states, as of this writing; on Unix-like platforms, it is the equivalent of $env:HOME
:
New-Item -ItemType Directory -Force -Path $HOME\mysubdirectory
As an aside: \
is not a metacharacter in PowerShell, so there's no reason to double it for literal use.
[1] Arguably, it should be the equivalent of ${env:HOMEDRIVE}${env:HOMEPATH}
instead, which is by default and typically the same as $env:USERPROFILE
, but can be configured to point to a different location - see this comment on GitHub issue #17685 for background information.