PowerShell: Compare Text Files And Export Different Entries


I am not a great script writer. But I love using script, as it is simple but very handy Smile

Scenario: I got a long list of machines (all.txt) that need to be processed, and part of them have already been processed by a guy. And he send me a list (processed.txt) that he have done. By using my following PowerShell script t, I can get the machine list that need to be done.

$file1 = “processed.txt”
$file2 = “all.txt”
$file3 = “unprocessed.txt”

foreach ($a in (Get-Content $file1))
{   
    foreach ($b in (Get-Content $file2))
    {
        if ($a -eq $b)
        {
        (Get-Content $file2) -replace “$b”,”” | Out-File $file2
        }
    }
}

(Get-Content $file2) | where {$_ -ne “”} | out-file $file3

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s