Powershell condition


I do not know how to do that. I'd like to add condition to this code below. I'd like the value "$visio" writes "none" if the software is not found. For the moment, if the software is not found, result is empty.

$visio = Get-WmiObject -Class Win32_Product | sort-object Name | select Name | where { $_.Name -match “visio”} |
    Format-table -HideTableHeaders |

Open in new window

Thank you in advance for your help


ChristopheIT CoordinatorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zephyr ICTCloud ArchitectCommented:
Do you mean something like this?

If ($visio = Get-WmiObject -Class Win32_Product | sort-object Name | select Name | where { $_.Name -match “visio”} | Format-table -HideTableHeaders | Out-String) {write-host "Visio Files found"}
Else {write-host "No Visio Files found"}

Open in new window

The write-host can be replaced by other things of course.
ChristopheIT CoordinatorAuthor Commented:
Thank you for your help. Yes it is like this but I'd like to write the result of value $visio in email body. I do not know how to use your code to do that. I will try
ChristopheIT CoordinatorAuthor Commented:
Because for the moment, I get result value in email. For exemple I receive "Microsoft Visio Viewer 2010"

If software with visio in name is not found, I'd like to receive "none".  But I need to keep the result of the code. ie visio version found.
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Zephyr ICTCloud ArchitectCommented:
Something like this? It will send mail on each occasion, I could probably simplify it more but I'm limited in time right now...

IF ($visio = Get-WmiObject -Class Win32_Product | sort-object Name | select Name | where { $_.Name -match “visio”} | Format-table -HideTableHeaders | Out-String) {Send-MailMessage -To <email> -From <email> -Subject "Visio Files" -body "Visio Files found" -SmtpServer <your-smtp-server>}
Else {Send-MailMessage -To <email> -From <email> -Subject "Visio Files" -body "No Visio Files found" -SmtpServer <your-smtp-server>}

Open in new window

Your inital code is overly complicated.
$visio = Get-WmiObject -Class Win32_Product | Where {$_.Name -match "Visio"} | Select -ExpandProperty Name
If (!$Visio) {$Visio = "Visio not found"}

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ChristopheIT CoordinatorAuthor Commented:
Thank you! it works perfectly. You are right, it was complicated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.