Powershell pipe to copy files that match a xpath query

I’m a powershell newb. There I said it. Now let me learn. Here’s what I created after some google searches.

  1. Go through all files in a directory
    Get-ChildItem . -r -file
  2. Filter on xml files
    Where { $_.Extension -EQ '.xml'}
  3. I think this can be added to the Get-ChildItem command, have fun optimizing this 🙂

  4. Take only the files that match a XPath
    Select-Xml -XPath "XPATH_HERE"
  5. you can test the xpath with online tools like this one

  6. Copy the file to a directory
    Copy-item -Destination DESTINATION_DIR_HERE

    this takes the Path from the Get-ChildItem, make sure to use -Destination to avoid parameter exception as described here

Complete line with pipes

Get-ChildItem . -r -file | Where { $_.Extension -EQ '.xml'} | Select-Xml -XPath "XPATH_HERE" | Copy-item -Destination DESTINATION_DIR_HERE

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Tooling and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.