19

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?

dwwilson66
  • 6,806
  • 27
  • 72
  • 117
  • are you sure that the whole code was run both times? That the lines setting `$inpath` and `$srcfiles` were both executed the second time with the new value? – Frode F. Jan 24 '13 at 13:52
  • they were executed with the new value, but BEHAVED as if they'd been passed the OLD value. – dwwilson66 Jan 24 '13 at 14:34

3 Answers3

26

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

CubanX
  • 5,176
  • 2
  • 29
  • 44
Patrick
  • 269
  • 3
  • 2
  • Remove-Module * is not too helpful. It should be followed with Import-Module otherwise – GeekzSG Jun 26 '15 at 06:31
  • 2
    Clearly the answer I was looking for. I've put this line at the top of my script and now I don't have to re/open PS ISE everytime. –  Aug 30 '17 at 08:21
14

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.

Frode F.
  • 52,376
  • 9
  • 98
  • 114
1

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

Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96