user2800034
user2800034

Reputation: 11

List just files in a Powershell script

I found a script that does great. The only change I want to make is to list just the files, not the new folders. This script should monitor a folder and subfolders and notify only when a new file has been created. How can I filter this down to just the file? Can I add an exclude on the get-childitem?

    Param (
    [string]$Path = "\\path\share",
    [string]$SMTPServer = "smtp server",
    [string]$From = "email",
    [string]$To = "email",
    [string]$Subject = "New File(s) Received on the FTP site"
    )

$SMTPMessage = @{
    To = $To
    From = $From
    Subject = "$Subject at $Path"
    Smtpserver = $SMTPServer
}

$File = Get-ChildItem $Path | Where { $_.LastWriteTime -ge (Get-Date).Addminutes(-10) }
If ($File)
{   $SMTPBody = "`nTo view these new files, click the link(s) below:`n`n "
    $File | ForEach { $SMTPBody += "$($_.FullName)`n" }
    Send-MailMessage @SMTPMessage -Body $SMTPBody

}

Thanks

Upvotes: 0

Views: 1485

Answers (1)

ama1111
ama1111

Reputation: 579

To list only files, you can pass the -File parameter to Get-ChildItem.

From Get-ChildItem for FileSystem:

To get only files, use the File parameter and omit the Directory parameter.

Upvotes: 0

Related Questions