Welcome › Forums › General PowerShell Q&A › How do I pass multiple wildcard strings to Where-Object? Script Help.
- This topic has 1 reply, 2 voices, and was last updated 5 months ago by
Participant.
Viewing 1 reply thread
-
AuthorPosts
-
-
August 25, 2020 at 11:08 am #252353
Hey all,
I found a script online that does almost exactly what I need it to and it does it quite fast as well. Essentially it pulls ALL the software installed on a machine(s) but I would like to find only instances of certain software titles. Here is the slightly modified version of the script:
<#.SYNOPSISGet-InstalledSoftware retrieves a list of installed software.DESCRIPTIONGet-InstalledSoftware opens up the specified (remote) registry and scours it for installed software. When found it returns a list of the software and it's version..PARAMETER ComputerNameThe computer from which you want to get a list of installed software. Defaults to the local host..EXAMPLEGet-InstalledSoftware DC1This will return a list of software from DC1. Like:Name Version Computer UninstallCommand---- ------- -------- ----------------7-Zip 9.20.00.0 DC1 MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}Google Chrome 65.119.95 DC1 MsiExec.exe /X{6B50D4E7-A873-3102-A1F9-CD5B17976208}Opera 12.16 DC1 "C:\Program Files (x86)\Opera\Opera.exe" /uninstall.EXAMPLEImport-Module ActiveDirectoryGet-ADComputer -filter 'name -like "DC*"' | Get-InstalledSoftwareThis will get a list of installed software on every AD computer that matches the AD filter (So all computers with names starting with DC).INPUTS[string[]]Computername.OUTPUTSPSObject with properties: Name,Version,Computer,UninstallCommand.NOTESAuthor: Anthony HowellTo add directories, add to the LMkeys (LocalMachine).LINK[Microsoft.Win32.RegistryHive][Microsoft.Win32.RegistryKey]#>Function Get-InstalledSoftware {Param([Alias('Computer', 'ComputerName', 'HostName')][Parameter(ValueFromPipeline = $True,ValueFromPipelineByPropertyName = $true,Position = 1)][string]$Name = $env:COMPUTERNAME)Begin {$lmKeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall", "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"$lmReg = [Microsoft.Win32.RegistryHive]::LocalMachine$cuKeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall"$cuReg = [Microsoft.Win32.RegistryHive]::CurrentUser}Process {if (!(Test-Connection -ComputerName $Name -count 1 -quiet)) {Write-Error -Message "Unable to contact $Name. Please verify its network connectivity and try again." -Category ObjectNotFound -TargetObject $Computer}else {$OS = (Get-CimInstance -ComputerName $Name -ClassName Win32_OperatingSystem).Caption$masterKeys = @()$remoteCURegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($cuReg, $computer)$remoteLMRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($lmReg, $computer)foreach ($key in $lmKeys) {$regKey = $remoteLMRegKey.OpenSubkey($key)foreach ($subName in $regKey.GetSubkeyNames()) {foreach ($sub in $regKey.OpenSubkey($subName)) {$masterKeys += (New-Object PSObject -Property @{"ComputerName" = $Name"Name" = $sub.getvalue("displayname")"SystemComponent" = $sub.getvalue("systemcomponent")"ParentKeyName" = $sub.getvalue("parentkeyname")"Version" = $sub.getvalue("DisplayVersion")"UninstallCommand" = $sub.getvalue("UninstallString")"InstallDate" = $sub.getvalue("InstallDate")"RegPath" = $sub.ToString()})}}}foreach ($key in $cuKeys) {$regKey = $remoteCURegKey.OpenSubkey($key)if ($regKey -ne $null) {foreach ($subName in $regKey.getsubkeynames()) {foreach ($sub in $regKey.opensubkey($subName)) {$masterKeys += (New-Object PSObject -Property @{"ComputerName" = $Name"Name" = $sub.getvalue("displayname")"SystemComponent" = $sub.getvalue("systemcomponent")"ParentKeyName" = $sub.getvalue("parentkeyname")"Version" = $sub.getvalue("DisplayVersion")"OS" = $OS"UninstallCommand" = $sub.getvalue("UninstallString")"InstallDate" = $sub.getvalue("InstallDate")"RegPath" = $sub.ToString()})}}}}}$woFilter = { $null -ne $_.name -AND $_.SystemComponent -ne "1" -AND $null -eq $_.ParentKeyName }$props = 'Name', 'Version', 'ComputerName', 'OS'$masterKeys = ($masterKeys | Where-Object $woFilter | Select-Object $props | Sort-Object Name)$masterKeys}End {}}Get-ADComputer -Filter { (Enabled -eq $true -and operatingSystem -like 'Windows*') } | Get-InstalledSoftwareWhat I’m trying to do is pipe this to Where-Object to filter for applications -Like ‘*Office Professional*’ -or ‘*Visio Professional*’ -or ‘*Roxio*’. I’ve tried using an array but found that -contains works like -match instead of -like. Any help is much appreciated.
-
This topic was modified 5 months ago by
NewToThisSOS09. Reason: Formatting of code
-
This topic was modified 5 months ago by
-
August 25, 2020 at 11:25 am #252359
Use regex OR:
PowerShell12345678910$test = @"NameMicrosft Office Non-ProfessionalMicosoft Office Professional 2020 X-TremeMicrosoft Visio Professional Platinum PlusRoxio Creator NXT Z1000 Super ProfessionalSuper Professional Plantinum Software Extra"@ | ConvertFrom-CSV$test | Where{ $_.Name -match 'Office Professional|Visio|Roxio' }Output:
PowerShell12345Name----Micosoft Office Professional 2020 X-TremeMicrosoft Visio Professional Platinum PlusRoxio Creator NXT Z1000 Super Professional
-
-
AuthorPosts
Viewing 1 reply thread
- The topic ‘How do I pass multiple wildcard strings to Where-Object? Script Help.’ is closed to new replies.