Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store variable and use it as a -Pattern from Get-Content?

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}

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading