PowerShell Get-ChildItem -Exclude and -Include not working
<p>Can someone tell me what is going on here?
I can get -Filter *.log to work no problem.</p>

<pre><code>PS C:\logs&gt; Get-ChildItem -Filter *.log -Recurse


    Directory: C:\logs


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/19/2016   2:45 PM          0 Moninitor-error-1.log
-a---         7/19/2016   2:45 PM          0 Moninitor-out-1.log
-a---         7/19/2016   2:45 PM          0 Watcher-error-3.log
-a---         7/19/2016   2:52 PM     264810 Watcher-out-3.log
-a---         7/19/2016   2:48 PM          0 FolderWatcher-error-2.log
-a---         7/19/2016   2:52 PM    7768537 FolderWatcher-out-2.log
-a---         7/19/2016   4:34 PM          0 nothing-error.log
-a---         7/19/2016   4:34 PM          0 nothing.log
-a---         7/18/2016   2:38 PM          0 log-error-0.log
-a---         7/19/2016   2:45 PM          0 log-out-0.log
</code></pre>

<p>as soon as I try to use -Include or -Exclude I get an access error.</p>

<pre><code>PS C:\logs&gt; Get-ChildItem -Exclude *.log -Recurse
Get-ChildItem : Access is denied
At line:1 char:1
+ Get-ChildItem -Exclude *.log -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand

PS C:\logs&gt; Get-ChildItem -Include *.log -Recurse
Get-ChildItem : Access is denied
At line:1 char:1
+ Get-ChildItem -Include *.log -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\...tor-error-1.log:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : GetItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access is denied
At line:1 char:1
+ Get-ChildItem -Include *.log -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand
</code></pre>

<p>What I am wanting to do is loop through the files in this directory and delete any that are older than 0 days, and don't contain error in the file name.</p>

<p>My current script is this.</p>

<pre><code>#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "0"
#----- define folder where files are located ----#
$TargetFolder = "C:\logs"
#----- define extension ----#
$Extension = "*.log"
$Skip = "*error*"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-ChildItem -Path $TargetFolder -Filter $Extension -Exclude $Skip -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files) 
    {
    if ($File -ne $NULL)
        {
        write-host "Deleting File $File" -ForegroundColor "DarkRed"
        Remove-Item $File.FullName | out-null
        }
    else
        {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        }
    }
</code></pre>

<p>But Anytime I sue Exclude or Include It gives me these Access Errors.</p>

<p>-Recurse on its own works perfectly as well...</p>

<pre><code>PS C:\Users\Administrator\.pm2\logs&gt; Get-ChildItem -Recurse


    Directory: C:\logs


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/19/2016   2:45 PM          0 Moninitor-error-1.log
-a---         7/19/2016   2:45 PM          0 Moninitor-out-1.log
-a---         7/19/2016   2:45 PM          0 Watcher-error-3.log
-a---         7/19/2016   2:52 PM     264810 Watcher-out-3.log
-a---         7/19/2016   2:48 PM          0 FolderWatcher-error-2.log
-a---         7/19/2016   2:52 PM    7768537 FolderWatcher-out-2.log
-a---         7/19/2016   4:34 PM          0 nothing-error.log
-a---         7/19/2016   4:34 PM          0 nothing.log
-a---         7/18/2016   2:38 PM          0 log-error-0.log
-a---         7/19/2016   2:45 PM          0 log-out-0.log
</code></pre>