Home > Enterprise >  Powershell variable - verbose if it is receiving input
Powershell variable - verbose if it is receiving input

Time:05-04

I have created a script that compress files older than N days using PowerShell. Like this:

param (
    $dirPath, `
    [int] $daysAgo, `
    $logOutput=$dirPath "\old_reports.log", `
    $fileExt
    )

$curDate = Get-Date
$timeAgo = ($curDate).AddDays($daysAgo)

$files = Get-ChildItem -Recurse `
                       -Path $dirPath `
                       -Include *.$fileExt| `
                       Where-Object { $_.LastWriteTime -lt $timeAgo } | `
                       Select -ExpandProperty FullName

& 'C:\Program Files\7-Zip\7Z.exe' a -t7z -mx9 old_reports.7z $files -bb1 -sdel

echo $files > $logOutput

It is working, but, since there are many files, it takes a while to fill the $files variable. While it is doing that, the prompt shows only a blinking cursor. Therefore, I am not aware if the script is actually doing something or it's paused by an accidental click.

Is there a way to show that $files variable is receiving input?

CodePudding user response:

Without restructuring your command - and thereby sacrificing performance - I see only one option:

In addition to capturing file-info objects in variable $files, print them to the display as well, which you can do with the help of the common -OutVariable parameter:

# Output the files of interest *and* capture them in 
# variable $files, via -OutVariable
Get-ChildItem -Recurse `
                       -Path $dirPath `
                       -Include *.$fileExt| `
                       Where-Object { $_.LastWriteTime -lt $timeAgo } | `
                       Select -ExpandProperty FullName -OutVariable files
  • Related