0

Trying to learn how to use Powershell to help with pulling data from various sources and banging my head on a poorly formed CSV. I have a CSV that just has a text string in the first cell of each row. Each row has a hash of a file, some data and the hash of the program that wrote it. I need to extract the first hash after "link hash=" below to another file.

[enter code here][1]

If the image doesn't work the first lines read:

The file "<share><link hash="randomhash1">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash2">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash3">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash4">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".

I have been looking for a way to tell powershell to give me the value for "hash=" "" in a new CSV, but hitting syntax errors and obviously just doing it tremendously wrong. As I am trying to learn this, if somebody could state this is the regex and why I would be eternally grateful.

Below is what I have tried. Looking at Select-String I have a knowledge gap on how to get the regex match to become an object I can output. If I take out the ForEach-Object argument I get a csv with a true statement for each line.

Import-Csv C:\Users\me\Desktop\Bad_hash.csv | Select-string -Pattern "(?\w+)" | ForEach-Object {$hash = $_.Matches[0].Groups['hash'].Value [PSCustomObject] @{ Hash = $hash}} | | Export-Csv C:\Users\me\Desktop\test.csv

12
  • You have not included any code or example data. Please update your question with sample CSV data as well as the script/code you are using as well as the output you are expecting. Commented Dec 9, 2020 at 17:26
  • Check out Select-String. Commented Dec 9, 2020 at 18:06
  • Your update only has the text you're looking for, which is good, but we really need a sample of the CSV file (shortened to a few lines and redacted appropriately, of course). You also haven't posted the script/code you are using to attempt to parse it. Commented Dec 9, 2020 at 21:20
  • @Bill_Stewart - Thanks I have been poking at Select-String to see if I could figure this out. Only about a month into learning PowerShell. I have put in what I have been working on. Commented Dec 9, 2020 at 22:14
  • You still not have included a CSV sample. Commented Dec 10, 2020 at 0:12

1 Answer 1

0

Select-String -pattern is your RegEx friend when Import-Csv isn't a viable option. I'd be more helpful if I had samples.

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

1 Comment

Updated the string, sorry for the delay as I was called off for other stuff.

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.