Does Powershell Have a Cache That Needs to Be Cleared?

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?

2

3 Answers

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

2

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.