Just Getting Specific Files in PoSh–#SQLNewBlogger

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

Just a quick one today, but a tip I hope I remember. While working on a script, I wanted to just get backup files in PoSh. I tried this:

Get-ChildItem -Path $BackupPath -File -Include *.bak

That got me this:

2019-09-23 10_06_19-cmd - powershell


Why not? Apparently, you need the –Recurse parameter, even though there are no subdirectories in this folder. A bug, IMHO, but who knows. In any case, adding the parameter makes things work. Thanks, SO.

2019-09-23 10_11_59-cmd - powershell


This was a quick 5 minute thing. After researching and testing for 10 minutes, and finding the solution, I wrote this up.

You could do that with anything you get working.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , . Bookmark the permalink.

1 Response to Just Getting Specific Files in PoSh–#SQLNewBlogger

  1. Agostino says:

    From Microsoft https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-6:

    When the Include parameter is used, the Path parameter needs a trailing asterisk (*) wildcard to specify the directory’s contents.

    So for example, Get-Childitem -Path C:\Test\* -include P*.pdf
    or more briefly Get-Childitem -Path C:\Test\P*.pdf



Comments are closed.