简体   繁体   中英

Powershell Help, Need to search for pattern/string and copy to new destination

I need Help to search a Network Share for 30+ Keywords and then to copy them to a new location... I need to search for all type of docs... txt, doc, docx, pdfs, xls, xlsx, etc... I have a CSV file of Keywords... the header is called Words FYI - In the CSV file I have each word is not in quotes and some lines does have two words (do they need quotes? a few have a wildcard hous* (for houses, house, housing, etc..) does that need quotes? example

street

1234 Elm St

Hous*

Do they need to be in Quotes?

It will wont search sub directories

This is what I have...

$CSV = Import-Csv -Path "C:\Users\Username\Documents\book1.csv"

foreach ($Words in $CSV)
{
    Get-ChildItem \\Server\Groups$\HR-Dept -Recurse | Select-String -Pattern '$Words' -CaseSensitive -SimpleMatch | Copy-Item -Destination "C:\Users\Username\Desktop\Testing"
}

If you're intending to search by file name rather than content, -filter does the job quite efficiently.

$CSV = Import-Csv -Path "C:\Users\Username\Documents\book1.csv"

foreach ($Words in $CSV) {
    # remove any extra "*" to avoid duplication later
    $words = $words -replace "*"
    # use -File to exclude directories from match. NB: PS version-dependent
    Get-ChildItem \\Server\Groups$\HR-Dept -filter *$words* -Recurse -File | Copy-Item -Destination "C:\Users\Username\Desktop\Testing"
}

Spaces in your $words should be fine if your file names will have the same words and spaces.

I recommend trying the gci part first to see if you get the appropriate list of files before pipelining it to the Copy-Item . I haven't tested with a file path that includes a $ , so that might be a consideration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM