How to access access a path in \AppData\Local without a variable ?

Hi All

Can anyone please tell me how to access/ write to the below path without using an environmental variable ?

C:\Users\logged in user\AppData\Local

The reason for this is it will be deployed via a scheduled task…and doing it that way does not allow the environmental variables…ie script runs but doesn’t make any changes, weird I know !

Many thanks for any info.

If it’s about getting the currently logged on user from a system account session you could query the registry.

$User = 
Get-Childitem -Path Registry::HKEY_USERS\ -ea 0 -exclude ".default"|
ForEach-Object {Get-ChildItem Registry::$_}|
Where-Object {$_.Name -like "*Volatile Environment"} | 
Get-ItemProperty -Name username

$User.Username would be the variable you can insert in your path then.

Hi Olaf
Thanks for the info…how would that work if I needed to write to that path then?

Get-Content $User.Username + "\AppData\Local\Microsoft\

The above does not work?
Is there any other easier method?

I am also aware of one way:

$username = (Gwmi -Class Win32_ComputerSystem).username
$path = "C:\users\" + $username + "\AppData\Local\Microsoft\

Did you actually read my answer?

 

I did yes…but I think you misunderstood…it’s not just the username I wanted.

It was the path to the current user local appdata.

 

 

 

OK, and what’s the only part of the path what differs from user to user? :wink: … if you still don’t know what I mean I recommend to play a little bit with it and to test a few things until you get what you want.

Got it thank you.

You dont give any details on what your script is actually doing, however you do mention:

“The reason for this is it will be deployed via a scheduled task…and doing it that way does not allow the environmental variables”

What user is the script running as? If it is the current user, these should work:

$ENV:AppData

$ENV:LocalAppData

 

[quote quote=289660]You dont give any details on what your script is actually doing, however you do mention:

“The reason for this is it will be deployed via a scheduled task…and doing it that way does not allow the environmental variables”

What user is the script running as? If it is the current user, these should work:

$ENV:AppData

$ENV:LocalAppData

[/quote]
The issue was due to the fact it was running as system…once I changed to user…it does indeed work with variables…that then caused another issue where the powershell window would show for a brief time…even with --WindowStyle Hidden

But sorted that by making a vbs call the powershell.

Thanks guys.

You can query the USERNAME of the currently logged on user even from inside a system session with the code I posted in my first answer. And when you insert this USERNAME in a path like this : "C:</span>users<strong>USERNAME</span>AppData</span>Local</span>Microsoft" you’ve got what you need. Is it really that hard to understand?

Olaf…I did confirm I understood and already thanked you for the info above ? comment no 289312

The issue is resolved…

Cheers and Many thanks