dwwilson66
dwwilson66

Reputation: 7066

Does Powershell have a cache that needs to be cleared?

This morning, I copied a directory from my local, networked drive to temp folder for testing. This error appeared.

Get-Content : Cannot find path 'C:\users\xxxxx\desktop\cgc\Automatic_Post-Call_Survey_-_BC,_CC.txt' because it does no
t exist.
At C:\users\xxxxx\desktop\cgc\testcountexcl1.ps1:55 char:12
+ Get-Content <<<<  $txtfile | Get-WordCount -Exclude (Get-Content c:\temp\exclude.txt) | select -First 15
    + CategoryInfo          : ObjectNotFound: (C:\users\xxxxx...ey_-_BC,_CC.txt:String) [Get-Content], ItemNotFoundEx
   ception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

This would be expected with the move...PS can't find the path referenced...but I made the following change prior to running the script (old commented out above the new):

$input = Get-Content c:\temp\wordCount.txt
<# $inpath = "C:\users\xxxxx\desktop\cgc\tx"    #>
$inpath = "C:\temp\tx"  
$srcfiles = Get-ChildItem $inpath -filter "*.txt"    
$notPermittedWords = Get-Content c:\temp\exclude.txt 

My first inkling is that there's some kind of cache holding my $inpath variable from my last run...but have not been able to find out if that's expected PowerShell behavior. Am I misinterpreting the error or the solution? How do I flush the cache or whatever varables may be stored in memory?

Upvotes: 20

Views: 85913

Answers (3)

Andreas Covidiot
Andreas Covidiot

Reputation: 4765

Additionally to @Patrick's answer you could remove designated modules like this:

,@("MyModule1","MyModule2") | %{&remove-module -erroraction:silentlycontinue $_}

pipe-way inspired from here

so Patrick's one-liner would become Remove-Variable * -ErrorAction SilentlyContinue; ,@("MyModule1","MyModule2") | %{&Remove-Module -ErrorAction:SilentlyContinue $_}; $error.Clear(); Clear-Host

Upvotes: 1

Patrick
Patrick

Reputation: 279

I do not like the idea of having to close and re-open every time I need to clear the cache.

This works in PowerShell

Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host

Upvotes: 27

Frode F.
Frode F.

Reputation: 54881

Variables are stored in the session, so if you close your powershell console and open a new one, all custom variables will be gone. If you want to see what variables exists, use Get-Variable . To delete a specific variable(to make sure it's gone), you could use:

Remove-Variable varname.

As for you question. A variable in the global(session) scope is stored in the session until it's removed or overwritten. If you used the same powershell console to run that code twice, then $inpath should have been set to "C:\temp\tx" the second time, and $srcfiles would be updated with the new filelist.

Upvotes: 14

Related Questions