Variables in a Path?

Hi all,

I have a little script that places some fields and values in an XML file which I need to roll out to the estate.

This works fine when I have the path defined explicitly thus:
$path = “C:\IS\XMLTest\user.config”

However the file I actually need to modify resides quite a way down in a users profile. In fact it is here:

Is there a way to use the %username% variable in $Path to replace the username part of the statement?

Many thanks.


%username% is not really PowerShellie.

Try to use this (assuming the code will be executed on the computer of the user):

$path = "C:\Users\username\AppData\Roaming\Mitel\UC\$\user.config"

Pay attention to the double quotes I’m using. I do that, because there is an expandable item in the path ($env:USERNAME).


Thank you so much for such a fast response.

Works perfectly. I am learning all the time. I thought there would be something amiss with the username bit.

With your comment on running on the computer of the user in testing yes. But I would need to run this as some form of logon script through Group Policy.

Are you saying this will not work?

When the script is run to perform work on user.config, how is it going to be run? If the script is running in the context as the user, then $env:Username will expand to that user’s name. However, if you are running as SYSTEM to install software, then $env:UserName will be most likely blank or show as SYSTEM. Basically, the answer is going to depend on how you are running the script on the workstation.

Ah yes. Thanks Rob. I will be running it as the user so this should work fine. Understood about the system running it would not work.

All I am doing is modifying a file that already exists in that path so needs to be under the user context anyway.

Many thanks both for your input.