Using Get-WmiObject win32_service | Where-Object

I read a script using the get-wim command to install, reinstall, see the running services, etc but somehow the script can hang for anywhere from a few minutes to 2 hours at a time. I need to replace the command with so that it does not use the Get-WmiObject win32_service command.

Sebahat,
Welcome to the forum. :wave:t4:

Do you have any question about your issue?

In general you should avoid using Get-WmiObject. Use Get-CimInstance instead. :wink:

If the script is not too big consider pasting it here into forums for review, but please point out troublesome lines.

Thank you
function Get-Win32Services()
{
Get-WmiObject win32_service | Where-Object {$_.PathName -like “$serviceExePath”}
}
function Get-RunningWin32Services()
then many call Get-Win32Services for calling stop the services and install the services…

I try using ciminstance too but not working

function Get-Win32Services()
{
Get-WmiObject win32_service | Where-Object {$_.PathName -like “ $serviceExePath ”}
}

When you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

Is there a particular reason why you’re not using Get-Service?

Unless that script needs to work remotely there is no reason to use Get-WmiObject nor Get-CimInstance
simply use Get-Service and Set-Service to modify service state.

And if it does need to work remotely then consider wrapping entire code into Invoke-Command

To uninstall or install service use sc.exe