Powershell是否有需要清除的缓存?

dww*_*n66 13 powershell caching

今天早上,我将一个目录从我的本地网络驱动器复制到临时文件夹进行测试.出现此错误.

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
Run Code Online (Sandbox Code Playgroud)

移动时会出现这种情况... PS无法找到引用的路径...但是在运行脚本之前我做了以下更改(旧的注释在新的上面):

$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 
Run Code Online (Sandbox Code Playgroud)

我的第一个问题是,$inpath我的上一次运行中存在某种缓存保存变量...但是无法确定是否存在预期的PowerShell行为.我误解了错误或解决方案吗?如何刷新缓存或内存中可能存储的任何可用代码?

小智 17

我不喜欢每次需要清除缓存时必须关闭并重新打开的想法.

这适用于PowerShell

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

  • 显然我正在寻找的答案。我已将此行放在脚本的顶部,现在我不必每次都重新/打开 PS ISE。 (2认同)

Fro*_* F. 13

变量存储在会话中,因此如果关闭PowerShell控制台并打开一个新变量,则所有自定义变量都将消失.如果要查看存在哪些变量,请使用Get-Variable.要删除特定变量(以确保它已消失),您可以使用:

Remove-Variable varname.

至于你的问题.全局(会话)范围中的变量存储在会话中,直到删除或覆盖为止.如果您使用相同的PowerShell控制台运行该代码两次,那么$inpath应该已经设置为"C:\temp\tx"第二次,并且$srcfiles将使用新文件列表进行更新.