简体   繁体   中英

Filtering contents in a CSV file in PowerShell

I have the following PowerShell script that I am using to determine what applications an end user has on their machine (in addition to the default apps installed by the image).

Currently, the script gathers a list of applications on the old machine, and pastes the list of default applications underneath it (from DefaultApps.csv). I then open it in Excel, add conditional formatting to find the duplicate entries and hide/delete those rows. I am then left with a list of what I need to install on the new user's computer, which doesn't include anything that is installed by the image.

What I would like to do, is modify the script so that PowerShell will filter out BOTH entries of any duplicate it finds. I have found a few spots on the web that spell out how to remove one of the entries, but that won't help me in this case. I pretty much want PowerShell to pull the application list from the remote PC, and then remove any entry that exists in DefaultApps.csv.

I'm not sure where to start with this, as I haven't done much with editing CSV files besides what is in this script. Can someone point me in the right direction on how to accomplish this? If you know what cmdlet/switch I should look at, or if you have a link to a page that explains this, that would be a good start.

$PCListOld = Get-Content F:\PCList-Old.txt
ForEach ($PC in $PCListOld) {
    $AppList = Get-WmiObject -Computer $PC Win32_Product | Sort-Object Name | Select-Object Name
    $AppList | Export-CSV C:\Scripts\AppLists\$PC.csv -NoTypeInformation
    Import-CSV C:\Scripts\AppLists\DefaultApps.csv | Export-CSV C:\Scripts\AppLists\$PC.csv -Append
}

I'm not sure where to start with this, as I haven't done much with editing CSV files

But you're not trying to edit a CSV file, soooo... don't put the default applications in the output, so you won't have to filter them out. And don't put an application in the output if it's in the default applications.

$DefaultApps = Import-CSV C:\Scripts\AppLists\DefaultApps.csv

$PCListOld = Get-Content F:\PCList-Old.txt
ForEach ($PC in $PCListOld)
{
    $AppList = Get-WmiObject -Computer $PC Win32_Product | Sort-Object Name | Select-Object Name
    $AppList | Where-Object {$_.Name -notin $DefaultApps.Name} | Export-CSV C:\Scripts\AppLists\$PC.csv -NoTypeInformation
}

Untested, but roughly that.

PS. NB.

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