Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

appdata variable not using local drive path

Hey Experts.  Need a little guidance as I can't seem to figure out why the script isn't using the proper drive letter when launched.  

Rename-Item -Path "%appdata%\microsoft\templates\normal.dotm" -NewName "%appdata%\microsoft\templates\normal.bak"

if ((get-childitem "%appdata%\microsoft\templates\Normal.dotm").LastWriteTime.Date -le (get-date '04/10/2017'))
{ copy-item O:\office2016\Normal.dotm "%appdata%\microsoft\templates" -Force }

Open in new window


When the script runs, the %appdata% variable isn't using the local c:\ drive and related path.  I've tried ' and " with no luck.  I appreciate your help!
Avatar of Devin Becker
Devin Becker
Flag of United States of America image

samiam41,

%AppData% is specific to the users, so it would have to be C:\Users\USER_NAME\%AppData%...

Hopefully this helps!

Devin Becker
Avatar of samiam41

ASKER

Thank you sir.  Any way to make that user_name a variable so it flows with the user running the script?
samiam41,

You should be able to create a $username variable and even allow users to input it

$username = Read-Host -Prompt "Enter the username you would like to use"

Open in new window


That should work, then you should just  have to change the path to something like:

Rename-Item -Path "C:\Users\$username\%appdata%\microsoft\templates\normal.dotm" -NewName "%appdata%\microsoft\templates\normal.bak"

Open in new window


I've never tried variables in a path, but it should work, if not, my suggestion would be to make a separate string variable that concatenates the entire path and then set the -Path to that string, and that should work as well.

Hope this helps,

Devin Becker
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not sure quantum computer AI system you are made up of oBdA but I'm seriously in awe of you.  Thanks for your help, explanation and professionalism.  You're a great teacher.

DB, thanks for your help as well!