11

I am struggling to get this simple PowerShell script to work. I have googled, but I can't find the answer.

As you can see, I am specifying the source and destination location.

The $filedate variable is doing a dateadd to get yesterdays date as the filename contains the date.

The $filter variable has the string I am searching on, including the date part.

The Get-ChildItem command works on its own, but when I apply the filter it returns nothing. What am I missing to get this to work?

$source = "C:\MSSQL.1\Backup\"
$destination = "D:\MSSQL.2\Backup\"
$filedate = (get-date).AddDays(-1).tostring('yyyyMMdd')
$filter = "FULL_(local)_Product_" + $filedate + "*"
Get-ChildItem -Path $source -filter $filter | Copy-Item -Destination $destination
2
  • The simplest conclusion is that the filter doesn't match. Could you please edit your question to add the following: what the $filter string actually equals, and the name of one of the files it SHOULD match. Commented Jul 5, 2011 at 9:57
  • Works fine for me. What do you get with: gci $source "FULL_(local)_Product_*" Commented Jul 5, 2011 at 11:26

1 Answer 1

23

Try filtering the list of files by using the Where-Object cmdlet and the -match operator:

$source = "C:\MSSQL.1\Backup\"
$destination = "D:\MSSQL.2\Backup\"
$filedate = (Get-Date).AddDays(-1).ToString("yyyyMMdd")
$filter = "FULL_(local)_Product_$filedate"
Get-ChildItem -Path $source | Where-Object { $_.Name -match $filter } | Copy-Item -Destination $destination

If you're still not getting any results, then we need to look at the filter itself.

Related resources:

Sign up to request clarification or add additional context in comments.

1 Comment

| Copy-Item -Destination $destination

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.