I'm trying to use powershell to Get-Content from a .txt file, then set-content after filtering the data using an array. I see plenty examples how to do this for 1 word, but not any word in an array. I have searched and tried adopting other solutions here and nothing is working for me. What can I put in my foreach loop to acommplish this? I feel like this will have a very simple answer and I'm overcomplicating it.
#Put account numbers into array.
$acctArr = "123456789101","121314151617","181920212223","242526272829"
#Use the array to Output a new NB file that only contains the accounts from the array.
$fileData = (Get-Content -Path "C:\testfile.txt") | Select-String -Pattern $acctArr -SimpleMatch
foreach($item in $fileData){
<# Need code here to figure out how to check the $fileData array for strings in $acctArr
then if a line of the file has one of the numbers in $acctArr let's do a set-content or Out-file of some kind #>
}
Edit: I tried solutions from this but set-content
appears to set the whole file, even lines that do not match what is in my $acctArr
Another Edit: I tried this also. I can write-host the $fileData
variable and it outputs to the screen correctly, but if I do a set-content, it doesn't write to the file like I expect:
$fileData = (Get-Content -Path "C:\Folder\testfile.txt") | Select-String -Pattern $acctArr -SimpleMatch
$fileData | Set-Content Output.txt