Running multiple sets of similar commands in one ps script

Hi all,

Our current automation tool has some built-in functionality to allow us to ‘cleanup’ our NAS folders. But we now have to move to a different tool that does not have that, so I have to use Powershell. The server has only PS ver 2 at this time. So, I have about 65 locations I monitor thru the automation to cleanup folders and files based on a certain age (how old they are). The first 2 in my test run are to delete entire directories, and the 2nd 2 affect only certain files types.

The first -WhatIf works fine showing exactly what it will delete once -WhatIf is removed.

But on the 2nd set of commands it stumbles with:

“Remove-Item : Cannot bind argument to parameter ‘Path’ because it is null”

The 2 sets of commands are identical with only the actual path being different. I literally did a copy\paste and changed that one literal string. Thus I am assuming I cannot execute multiple sets of commands like this in one script. My assumption is that I must use a different approach to somehow ‘separate’ these commands.

Script:

$HowOld = -30
#Path to the root folder
$Path = “\WDCNAQ513\NAS_Path1\Process_In”
$LastWrite = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
write-output $Path
Write-Output $LastWrite
#Delete folders older than Threshold Date
Get-ChildItem -Path $Path | Where-Object { $.PSIsContainer -and ($.LastWriteTime -lt $LastWrite)} | Remove-Item -Force -Recurse -WhatIf

$HowOld = -30
#Path to the root folder
$Path = “\WDCNAQ513\NAS_Path2\Process_In”
$LastWrite = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
write-output $Path
Write-Output $LastWrite
#Delete folders older than Threshold Date
Get-ChildItem -Path $Path | Where-Object { $.PSIsContainer -and ($.LastWriteTime -lt $LastWrite)} | Remove-Item -Force -Recurse -WhatIf

$HowOld = -365
$Path = “\WDCNAQ513\NAS_Path3\In”
$CreateDate = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
write-output $Path
Write-Output $CreateDate
#Get the files to delete
$filesToDelete = Get-ChildItem -Path $Path | Where-Object {$.Name -like “*.pdf”} | Where {($.creationtime -lt $CreateDate)}
$filesToDelete.Count
$filesToDelete | Remove-Item -Force -WhatIf

$HowOld = -180
$Path = “\WDCNAQ513\NAS_Path3\In”
$CreateDate = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
write-output $Path
Write-Output $CreateDate
#Get the files to delete
$filesToDelete = Get-ChildItem -Path $Path | Where-Object {$.Name -like “*.log”} | Where {($.creationtime -lt $CreateDate)}
$filesToDelete.Count
$filesToDelete | Remove-Item -Force -WhatIf

 

Any assistance would be greatly appreciated.

The first thing that comes to mind is that there are no records found in your GET and then you pipe NULL to Remove. Typically, you shouldn’t just pipe from a GET to something performing an action. Something like your 3rd section:

$filesToDelete = Get-ChildItem -Path $Path | 
                 Where-Object {($_.Name -like "*.pdf")-and ($_.creationtime -lt $CreateDate)}

'Found {0} files to delete in path {1}' -f $filesToDelete.Count, $path

if ($filesToDelete) {
    $filesToDelete | Remove-Item -Force -WhatIf
}

This approach we do a query, write output on the results and then if there is anything to process we pipe that to Remove.

Thank you very much. That is a good thought. The 2nd directory was indeed empty. I can try adding that logic.

That seemed to work! I also added an else statement:

if ($filesToDelete) {
$filesToDelete | Remove-Item -Force -WhatIf
} else {
Write-Output “Nothing to process”
}

 

Thanks again!