Trying to check if few software are installed, when I use this code, it works fine:
$Pattern = "Winr","adobe";$software = get-package | Select-Object Name; $array = @($software.name) | Set-Content C:\temp\installed.txt; $SEL = Select-String -Path C:\temp\installed.txt -Pattern $Pattern; if ($SEL -ne $null) {write-host Found => $SEL.Line} else {write-host Nothing Found}
This code works without issues, but when I try to get the variables from a text file, it is not working with this code:
$Pattern = Get-Content C:\temp\list.txt; $software = get-package | Select-Object Name; $array = @($software.name) | Set-Content C:\temp\installed.txt; $SEL = Select-String -Path C:\temp\installed.txt -Pattern $Pattern; if ($SEL -ne $null) {write-host Found => $SEL.Line} else {write-host Nothing Found}
The text file contain the following text:
"Winr","adobe"
How to use the content of the text file as a -Pattern in Select-String
Any idea to solve this issue?
Regards
>Solution :
list.txt should look like:
winr
adobe
for pattern to be an array of strings:
$pattern = get-content list.txt
select-string -pattern $pattern -path installed.txt
Note that you can simply say:
get-package *winr*,*adobe*
Since get-package accepts a string array of names and you can use wildcards. (Powershell 5.1 only for msi and programs providers.)
