# LOCALAPPDATA is NULL

**URL:** <https://forums.powershell.org/t/localappdata-is-null/23967>\
**Category:** PowerShell Help\
**Created:** [April 17, 2024, 2:54pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967 "2024-04-17T14:54:42Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 17, 2024, 2:54pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/1 "2024-04-17T14:54:42Z")

</div>

I’m executing a script remotely using a service account. I can’t run anything unless the service account is actually logged in on the server. The problem is the LOCALAPPDATA variable is blank and C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule is being executed. It’s failing on Line 95 stating -Path is NULL

$script:PSGetAppLocalPath = Microsoft.PowerShell.Management\Join-Path -Path $env:LOCALAPPDATA -ChildPath 'Microsoft\Windows\PowerShell\PowerShellGet'

If I log into the server as the service account and then execute the script remotely, it works fine. How can I get this to work using the Service Account without having to be logged in on the server? This will be a scheduled job that runs daily.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 17, 2024, 8:10pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/2 "2024-04-17T20:10:46Z")

</div>

Hello, welcome to the forum. You should post what you’re trying to run that is causing this error. PowershellGet is used to install modules/scripts. I would guess you are installing or importing something and you may not need to. Also, powershellget 2.2.5 is the latest stable version, perhaps you should update that and see if that doesn’t resolve your issue.

as admin, run

```powershell
Update-Module powershellget

```

You can confirm the version with

```powershell
Get-Module powershellget -ListAvailable

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [April 17, 2024, 10:22pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/3 "2024-04-17T22:22:25Z")

</div>

> [@HsbHsb](#):
>
> an’t run anything unless the service account is actually logged in on the server. The problem is the LOCALAPPDATA variable is blank and C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule is being executed. It’s failing on Line 95 stating -Path is NULL

Your title states ‘localappdata is null’. From your explanation it seems like you’re stating that localappdata envrionemtnal variable (the path) is null unless you login. I’m not able to replicate that. For example, let’s take a step back (often helpful when trying to troubleshoot) and confirm it’s truly null:

```powershell
Invoke-Command -ComputerName $servername -ScriptBlock {$env:localappdata}

```

That returns my localappdata path, without being logged in via RDP or locally to `$servername`.

EDIT: I would say it’s possible that you have to login to create a profile potentially before that value would return properl (as in log in once to create the local profile). I was trying to find a server that I had not logged into yet in my environment with one of my admin accounts but unfortunately it looks like i’ve logged into everyone I’ve checked so far, to try and confirm that.

So as Doug mentioned, could you further clarify what exactly you’re doing and share your entire script.

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 19, 2024, 8:14pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/4 "2024-04-19T20:14:16Z")

</div>

I’m using Powershell to get Teams Call Usage logs. Using SQL Server, I have a job that executes as a specific Service Account. I get the error unless the job starts while I’m logged into the server as the service account.

Start-Transcript -Path “xxxxxxxxxxxxxxxxxxxxxx\log.txt”

#Trust the repository used to prevent required user input  
Set-PSRepository -Name “PSGallery” -InstallationPolicy Trusted

Install-Module -Name TeamsCloudCommunicationApi -Scope CurrentUser

#Variables  
$ClientID = “xxxxxxxxxxxxxxxxxxxxxxxxxxxx”  
$ClientSecret = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”  
$TenantID = “xxxxxxxxxxxxxxxxxxxxxxxxxx”

$today = (Get-Date).AddDays(0).ToString(‘yyyy-MM-dd’)  
$yesterday = (Get-Date).AddDays(-7).ToString(‘yyyy-MM-dd’)  
$startTime = “T00:00:00.000”  
$endTime = “T23:59:59.999”

$auditStart = -join($yesterday,$startTime)  
$auditEnd = -join($yesterday,$endTime)

#Create secure string for password  
$ClientSecurePassword = ConvertTo-SecureString $ClientSecret -Force -AsPlainText  
$ClientCredential = New-Object Management.Automation.PSCredential($ClientID, $ClientSecurePassword )

$graphApiCreds = $ClientCredential  
#Get-Credential

$accessToken = Get-GraphApiAccessToken -Credential $graphApiCreds -TenantId $TenantID

Get-TeamsPstnCalls -StartDate $yesterday -EndDate $today -AccessToken $accessToken | Export-Csv “xxxxxxxxxxxxxxxxxxxxxxxxx.csv” -NoTypeInformation

Stop-Transcript

The log would then spit out:  
PS\>TerminatingError(Join-Path): “Cannot bind argument to parameter ‘Path’ because it is null.”  
Join-Path : Cannot bind argument to parameter ‘Path’ because it is null.  
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule.psm1:95 char:81

- … osoft.PowerShell.Management\Join-Path -Path $env:LOCALAPPDATA -ChildP …
- 

```
                                            ~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidData: (🙂 [Join-Path], ParameterBindingValidationException
  - FullyQualifiedErrorId :  
ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand  
The variable ‘$script:PSGetAppLocalPath’ cannot be retrieved because it has not been set.  
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule.psm1:102 char:86

- … werShell.Management\Join-Path -Path $script:PSGetAppLocalPath -ChildP …

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 19, 2024, 8:19pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/5 "2024-04-19T20:19:24Z")

</div>

Here’s the output from the -ListAvailable after updating

```
Directory: C:\Users\MyServiceAccount\Documents\WindowsPowerShell\Modules

```

ModuleType Version Name ExportedCommands

* * *

Script 2.2.5 PowerShellGet {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap…

```
Directory: C:\Program Files\WindowsPowerShell\Modules

```

ModuleType Version Name ExportedCommands

* * *

Script 2.2.4 PowerShellGet {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap…  
Script 1.0.0.1 PowerShellGet {Install-Module, Find-Module, Save-Module, Update-Module…}

PS C:\Windows\system32\>

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [April 22, 2024, 4:59am UTC](https://forums.powershell.org/t/localappdata-is-null/23967/6 "2024-04-22T04:59:07Z")

</div>

Format your code.

[How to format code on PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif)

Sometimes, funky things can happen if all the code isn’t formatted as code.

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 22, 2024, 1:31pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/7 "2024-04-22T13:31:47Z")

</div>

```auto
Start-Transcript -Path “xxxxxxxxxxxxxxxxxxxxxx\log.txt”

#Trust the repository used to prevent required user input
Set-PSRepository -Name “PSGallery” -InstallationPolicy Trusted

Install-Module -Name TeamsCloudCommunicationApi -Scope CurrentUser

#Variables
$ClientID = “xxxxxxxxxxxxxxxxxxxxxxxxxxxx”
$ClientSecret = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
$TenantID = “xxxxxxxxxxxxxxxxxxxxxxxxxx”

$today = (Get-Date).AddDays(0).ToString(‘yyyy-MM-dd’)
$yesterday = (Get-Date).AddDays(-7).ToString(‘yyyy-MM-dd’)
$startTime = “T00:00:00.000”
$endTime = “T23:59:59.999”

$auditStart = -join($yesterday,$startTime)
$auditEnd = -join($yesterday,$endTime)

#Create secure string for password
$ClientSecurePassword = ConvertTo-SecureString $ClientSecret -Force -AsPlainText
$ClientCredential = New-Object Management.Automation.PSCredential($ClientID, $ClientSecurePassword )

$graphApiCreds = $ClientCredential
#Get-Credential

$accessToken = Get-GraphApiAccessToken -Credential $graphApiCreds -TenantId $TenantID

Get-TeamsPstnCalls -StartDate $yesterday -EndDate $today -AccessToken $accessToken | Export-Csv “xxxxxxxxxxxxxxxxxxxxxxxxx.csv” -NoTypeInformation

Stop-Transcript

```

Formatted code does the same thing.

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [April 22, 2024, 2:53pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/8 "2024-04-22T14:53:57Z")

</div>

One more formatting thing, even though i don’t think it’s related: you gotta be careful about those quote characters. The ones in your code block won’t be interpreted by Powershell correctly. Here’s the same code with all the incorrect quote characters replaced with correct UTF8 ones.

```auto
Start-Transcript -Path "xxxxxxxxxxxxxxxxxxxxxx\log.txt"

#Trust the repository used to prevent required user input
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

Install-Module -Name TeamsCloudCommunicationApi -Scope CurrentUser

#Variables
$ClientID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$TenantID = "xxxxxxxxxxxxxxxxxxxxxxxxxx"

$today = (Get-Date).AddDays(0).ToString('yyyy-MM-dd')
$yesterday = (Get-Date).AddDays(-7).ToString('yyyy-MM-dd')
$startTime = "T00:00:00.000"
$endTime = "T23:59:59.999"

$auditStart = -join($yesterday,$startTime)
$auditEnd = -join($yesterday,$endTime)

#Create secure string for password
$ClientSecurePassword = ConvertTo-SecureString $ClientSecret -Force -AsPlainText
$ClientCredential = New-Object Management.Automation.PSCredential($ClientID, $ClientSecurePassword )

$graphApiCreds = $ClientCredential
#Get-Credential

$accessToken = Get-GraphApiAccessToken -Credential $graphApiCreds -TenantId $TenantID

Get-TeamsPstnCalls -StartDate $yesterday -EndDate $today -AccessToken $accessToken | Export-Csv "xxxxxxxxxxxxxxxxxxxxxxxxx.csv" -NoTypeInformation

Stop-Transcript  

```

now for your actual problem. I only did a couple of tests by comparison where I used an account to leverage invoke-command against a couple of computers I have never used before, so I wouldn’t have a profile on them. All I did was ask it to return the `$ENV:LOCALAPPDATA` variable which is did correctly. Even if i run psexec interactively to launch Powershell that session running as NT AUTHORITY\SYSTEM still has a value in `$ENV:LOCALAPPDATA`.  
My suspicion is that it’s something to do with how your script is being executed and by whom.  
You said you’re running the script remotely as a service account, can you provide a little bit more detail about _how_ you’re running it remotely?

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 26, 2024, 11:32am UTC](https://forums.powershell.org/t/localappdata-is-null/23967/9 "2024-04-26T11:32:03Z")

</div>

I’m using SQL Server Integration Services to run a package that calls an Execute Process Task.

Executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Arguments: -ExecutionPolicy ByPass -File “\PathToMyScript\MyScript.ps1”

In SQL Server Agent, I have the step set to run as my service account.

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 26, 2024, 2:14pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/10 "2024-04-26T14:14:42Z")

</div>

I tried to test using Write-Output to the log to get the value of $ENV:LOCALAPPDATA at the top of my script. Here’s the output:

Transcript started, output file is \PathToMyLogFile\log-test.txt  
Local App Data:  
PS\>TerminatingError(Join-Path): “Cannot bind argument to parameter ‘Path’ because it is null.”

Here’s the code I added to my script:

```auto
Start-Transcript -Path "\\........\log-test.txt"

Write-Output "Local App Data: "
Write-Output $ENV:LOCALAPPDATA

#Trust the repository used to prevent required user input
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

```

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 26, 2024, 8:25pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/11 "2024-04-26T20:25:15Z")

</div>

Is there a downside to hardcoding the value in my script?

`$ENV:LOCALAPPDATA = "C:\Users\{MYUSERNAME}\AppData"`

If I put this at the top, it works perfectly.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 26, 2024, 8:38pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/12 "2024-04-26T20:38:50Z")

</div>

> [@HsbHsb](#):
>
> ```auto
> $ENV:LOCALAPPDATA = "C:\Users\{MYUSERNAME}\AppData"
> 
> ```

It should be

```auto
$ENV:LOCALAPPDATA = "C:\Users\{MYUSERNAME}\AppData\Local"

```

But you should not set environment variables like this. I’d recommend to go with a regular variable isntead …

> [@HsbHsb](#):
>
> Is there a downside to hardcoding the value in my script?

You don’t have to …

```auto
$Path = 'C:\Users\{0}\AppData\Local' -f $ENV:USERNAME

```

> [@HsbHsb](#):
>
> I’m executing a script remotely using a service account. I can’t run anything unless the service account is actually logged in on the server. The problem is the LOCALAPPDATA variable is blank

You will have to run the script in your context - not in the context of a service account - to get your `$ENV:LOCALAPPDATA`!!! ☝🏼 🤷🏼‍♂️

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 29, 2024, 11:52am UTC](https://forums.powershell.org/t/localappdata-is-null/23967/13 "2024-04-29T11:52:43Z")

</div>

I think I need to set the environment variable because the code I’m executing is calling C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule.psm1 and the error I’m getting is because this file is referencing $env:LOCALAPPDATA in the following manner:

`$script:PSGetAppLocalPath = Microsoft.PowerShell.Management\Join-Path -Path $env:LOCALAPPDATA -ChildPath 'Microsoft\Windows\PowerShell\PowerShellGet\'`

If I set a regular variable, this will still be blank and fail here.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 29, 2024, 12:00pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/14 "2024-04-29T12:00:33Z")

</div>

> [@HsbHsb](#):
>
> I think …

??  
😏  
!!

> [@HsbHsb](#):
>
> the code I’m executing is calling C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule.psm1

That’s not in LocalAppData!!

> [@HsbHsb](#):
>
> this file is referencing $env:LOCALAPPDATA

So don’t use it! 🤷🏼‍♂️

> [@HsbHsb](#):
>
> $script:PSGetAppLocalPath = Microsoft.PowerShell.Management\Join-Path -Path $env:LOCALAPPDATA -ChildPath 'Microsoft\Windows\PowerShell\PowerShellGet'

IMO That’s a very bad idea anyway. Install the necessary modules in a path where all users can use it. 🤷🏼‍♂️

Whatfor do you need this actually? A proper installed PS module will be loaded automatically. You don’t have to reference its path in any way.

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 29, 2024, 12:20pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/15 "2024-04-29T12:20:56Z")

</div>

Yes, I think, because I’m new to Powershell.

I just went through the code and I don’t see a reference to PSModule.psm1 so it appears this is being called automatically and I’m not sure what the purpose of it is or if there’s a way to avoid and still run my code successfully. I see it is installed on every machine that I have Powershell on, regardless if that machine has run the code that I’m trying to run here.

Searching for PSModule.psm1 in this forum, it looks like the  
`Install-Module -Name TeamsCloudCommunicationApi -Scope CurrentUser`  
is calling PSModule.psm1

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 29, 2024, 12:49pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/16 "2024-04-29T12:49:15Z")

</div>

@Olaf

I was able to get the exact error message just by running the following. If I remove the Install-Module line, the error goes away, but that prevents me from installing what I need.

```auto
Start-Transcript -Path "\\MY_LOG_PATH\PSTN_Log.txt"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Install-Module PowershellGet -Force

Stop-Transcript

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 29, 2024, 1:29pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/17 "2024-04-29T13:29:23Z")

</div>

> [@HsbHsb](#):
>
> I’m new to Powershell.

There’s absolutely no shame to that. We’ve ALL been there once. 🤷🏼‍♂️

> [@HsbHsb](#):
>
> I just went through the code and I don’t see a reference to PSModule.psm1

You should not run code you don’t understand. You may start reading this:

> **[about Modules - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7.4)**
>
> Explains how to install, import, and use PowerShell modules.

… and usually you wouldn’t see any reference to the module anyway … but I’d expect there are some functions from that module in use.

> [@HsbHsb](#):
>
> Searching for PSModule.psm1 in this forum, it looks like the  
> `Install-Module -Name TeamsCloudCommunicationApi -Scope CurrentUser`  
> is calling PSModule.psm1

Hmmm … unlikely. 🤔 🤨

> [@HsbHsb](#):
>
> but that prevents me from installing what I need.

What is it actually what you need? What is the general purpose of your code/script?

And just to have it at least mentioned once:

> [@HsbHsb](#):
>
> `Start-Transcript -Path "\\MY_LOG_PATH\PSTN_Log.txt"`

That’s a very bad idea. PowerShell is quite slow when it comes to file system operations anyway. Doing it to a network share will slow down your code even further.

If you have to log - do it locally.

---

<div class="post-metadata">

**Author:** ![HsbHsb](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@HsbHsb](https://forums.powershell.org/u/HsbHsb)\
**Post date:** [April 29, 2024, 3:03pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/18 "2024-04-29T15:03:03Z")

</div>

I put the log in there initially to figure out why it wasn’t doing what it should. Once resolved, I can remove it but it’s just for testing.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 29, 2024, 3:03pm UTC](https://forums.powershell.org/t/localappdata-is-null/23967/19 "2024-05-29T15:03:09Z")

</div>

Closed on May 29, 2024, 3:03pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
