How can I return app details from a function in PowerShell?

0
14
Asked By TechWhiz123 On

I'm working on a PowerShell script to check if a specific application, like Google Chrome, is installed by looking at the registry uninstall keys. The function I'm using, `CheckApp`, currently returns a boolean value to indicate whether the app is found. However, I want to return additional details such as `DisplayVersion`, `InstallDate`, and `PSPath` of the app when it is installed. How can I modify my function to pass these details to the second part of my script?

4 Answers

Answered By DevGuru88 On

Instead of returning a boolean value, you should return the `$item` object that contains all the needed properties. This way, in your second part of the script, you can directly access `DisplayVersion`, `InstallDate`, and `PSPath`. Update your condition to check if the result is not `$false` instead of checking for true.

Answered By CodeNinja77 On

To return more information than just true or false, you can modify the `CheckApp` function to return a PSCustomObject with the app details. Instead of returning `$true`, return the `$item` when the matching display name is found. Then, in the second part of your script, check if the result is not `$null` to see if the app is installed and access the properties directly from the returned object.

Answered By ScriptMaster42 On

One way to achieve this is by changing your `CheckApp` function to return the `$item` object. This object contains all the custom properties like `DisplayVersion` and others which you can use later. Here's a quick snippet:

```powershell
function CheckApp {
# ... your existing paths code ...
foreach ($item in $items) {
if ($item.DisplayName -like "*$AppName*") {
return $item
}
}
return $null
}
```

Answered By PowerShellPro99 On

If you'd like to keep things simple, you could just have a separate function that handles the notification part using the properties from the registry. Your `CheckApp` function can find the app, and then you pass the found item to this new function that sends the data to Teams.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.