2

I get a "path is not valid", even though it's valid. I run it with my own user, and i can browse the folders and delete files in the folders. But when I run my code, I get "path is not valid". I have also tried running it in the administrative console. Am I doing something wrong? Powershell version: 4

param (  
    $rootPath = "$env:ProgramFiles\NetApp\SnapManager for Exchange\Report\",
    $paths = ("Debug [11EXS05]\","Backup [11EXS05]\"),
    $wildCard = "*.txt",
    $daysToKeep = "14"
)

#define LastWriteTime parameter based on $daysToKeep
$now = Get-Date
$lastWrite = $now.AddDays(-$daysToKeep)

#Loop through each path in paths
foreach($path in $paths)
{
    $currentPath = Join-Path -Path $rootPath $path    
    if(Test-Path $currentPath)
    {
        #get all the files that has LastWriteTime less than today minus $daysToKeep, then delete those files, pipe output to screen
        Get-ChildItem $currentPath `
            -Include $wildCard -Recurse | `
            Where {$_.LastWriteTime -le "$lastWrite"} | foreach{ "Removing file $($_.FullName)"; Remove-Item $_}
    }
    else
    {
        Write-Host ($currentPath + " is not valid") -ForegroundColor Red
    }

}

Edit, the solution:

Had to fix the [] like the answers below. And also, I had to add $currentPath to the Remove-Item command, the full script:

param (  
    $rootPath = "$env:ProgramFiles\NetApp\SnapManager for Exchange\Report\",
    $paths = ('Debug `[11EXS05`]\','Backup `[11EXS05`]\'),
    $wildCard = "*.txt",
    $daysToKeep = "17"
)

#define LastWriteTime parameter based on $daysToKeep
$lastWrite = (Get-Date).Date.AddDays(-$daysToKeep)

#Loop through each path in paths
foreach($path in $paths)
{
    $currentPath = Join-Path -path $rootPath $path

    if(Test-Path $currentPath)
    {
        #get all the files that has LastWriteTime less than today minus $daysToKeep, then delete those files, pipe output to screen
        Get-ChildItem $currentPath `
            -Filter $wildCard -Recurse | `
            Where {$_.LastWriteTime -le "$lastWrite"} | foreach{ "Removing file $($_.FullName)"; Remove-Item ($currentPath + "\" + $_) -Force}        

    }
    else
    {
        Write-Host ($currentPath + " is not valid") -ForegroundColor Red
    }
}
Svein Erik
  • 531
  • 2
  • 11
  • 24

2 Answers2

2

It's the square brackets in the folder name that are giving you a problem. See https://stackoverflow.com/a/21008352/1001100. You need to put a backtick before each square bracket. And since PowerShell uses the backtick as an escape character, you need two backticks if you are going to use double-quoted strings.

Change your second line to this:

$paths = ("Debug ``[11EXS05``]\","Backup ``[11EXS05``]\"),

Or this, if you want to switch to singly-quoted strings:

$paths = ('Debug `[11EXS05`]\','Backup `[11EXS05`]\'),

and it will work.

Community
  • 1
  • 1
JamesQMurphy
  • 4,214
  • 1
  • 36
  • 41
0

Issue may be with $RootPath

$rootPath = "$env:ProgramFiles\NetApp\SnapManager for Exchange\Report\",

$env: or "$env:HOMEDRIVE"+"\ProgramFiles\NetApp\SnapManager for Exchange\Report"

$env: call is incorrect..

(OR)

$paths = ('Debug `[11EXS05`]\','Backup `[11EXS05`]\')
VGSandz
  • 144
  • 5