2

I have an out-gridview implemented in my powershell script which shows me all files today created. If there are files in the specific path it works fine but if not nothing happens.

It would be great that the grid appears even if the directory doesn't contain any files. Either the grid lists no items or just maybe a notification that no files have been found.

example:

gci C:\User\Executions\2018-01-25 | Out-GridView

Everything would be nicer than nothing :-)

Sure, I could use Test-Path to query and write anywhere (e.g. Write-Host) but it is more aesthetic to output the message in a grid.

3 Answers3

1
$list = Get-ChildItem "C:\User\Executions\2018-01-25"
if(($list).count -gt 0){
  Get-ChildItem $list | Out-GridView
}else{
  'No Data found' | Out-GridView
}

@TheIncorrigible1 thank you!

0

Looks like someone beat me to it, but I'll include my example as well. Mine uses services, however. On my machine, if the Get-Service command uses -Name s*, it'll open Out-GridView with the services that begin with S. If the Get-Service command uses -Name x*, it'll run the Else portion and open Out-GridView with the PSCustomObject. What this version does give you is the ability to label the column. In Danijel de Vasco's example it uses a default of "string" as the column header vs. mine, which uses "Message." More or less the same thing, however, with a minor customization.

If (Get-Service -Name s* -OutVariable Services) {
    $Services | Out-GridView
} Else {
    $Message = [PSCustomObject]@{
        Message = 'No Files Found'
    }
    $Message | Out-GridView
}
tommymaynard
  • 1,981
  • 12
  • 14
-1

I am feeling generous today

try
{
    $out = gci C:\User\Executions\2018-01-25
    if ($out)
    {
        $out | Out-GridView
    }
    else
    {
        $null = [System.Windows.Forms.MessageBox]::Show("Directory is empty", "Notification", "OK", "Information")
    }
}
catch
{
    $ErrMsg = $_.Exception.Message
    $null = [System.Windows.Forms.MessageBox]::Show("Error Occurred: $ErrMsg", "Error", "OK", "Error")
}
Sid
  • 2,586
  • 1
  • 11
  • 22