Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell concatenate an Environment variable with path

So I have this code:

$userprofile=Get-ChildItem Env:USERPROFILE
$localpath="$userprofile\some\path"

I would expect output of the below from $localpath:

c:\users\username\some\path

However what I get is:

System.Collections.DictionaryEntry\some\path

So, of course, something like cd $localpath fails. How would I accomplish what I need?

like image 757
KyferEz Avatar asked Dec 08 '16 19:12

KyferEz


1 Answers

A convenient way to obtain the string value rather than the dictionary entry (which is technically what Get-ChildItem is accessing) is to just use the variable syntax: $Env:USERPROFILE rather than Get-ChildItem Env:USERPROFILE.

$localpath = "$env:USERPROFILE\some\path"

For more information:

PowerShell Environment Provider

about_Environment_Variables

Also, the Join-Path cmdlet is a good way to combine two parts of a path.

$localpath = Join-Path $env:USERPROFILE 'some\path'
like image 129
John K Avatar answered Oct 19 '22 09:10

John K